Error in python code.
Am getting weird error output, from the following code, in Python, on spyder:
Code:
input_dir = "D:\Screenshots\bk\ch_1" Code:
runfile('C:/Users/HP/untitled0.py', wdir='C:/Users/HP') |
Quote:
https://docs.python.org/3/reference/...cape-sequences |
You can also use the raw string operator r so the backslash (\) is treated as a literal character:
Code:
input_dir = r 'D:\Screenshots\bk\ch_1' Code:
path = os.chdir(input_dir) |
Thanks all, for the help.
The code works, but it is still erroneous, as on second run of it, on the stated folder, gives a weird error. Though the error is not important, but it is sure that, in the second run of the program, the file named: 1_10.png, is accessed instead of the file named: 1_2.png. Though immaterial, the same error is common if open images, in Paint, in a folder. I feel that the actual error, i.e.: FileExistsError, is immaterial here, though request a way to overcome that too. That way must be to overwrite the file, hence is wrong if the file named: 1_10.png, is accessed before the file named: 1_2.png. Code:
# -*- coding: utf-8 -*- Code:
runfile('C:/Users/HP/.spyder-py3/a.py', wdir='C:/Users/HP/.spyder-py3') Code:
runfile('C:/Users/HP/.spyder-py3/a.py', wdir='C:/Users/HP/.spyder-py3') edit: If the first run did not have the files named 'suitably', i.e. the file name '1_10.png' did not exist; then the first run would have faced the same difficulty. This makes the problem very serious. |
AS you added additional code after solving the first issue I would have said this should be a new question.
My thought would be, maybe the rename you have used did something to mess it up?? |
Quote:
|
Quote:
|
You are arbitrarily renaming files based on the order of the output of listdir which wasn't the same from the first run to the second. Plus as posted you have multiple similar files already. listdir as far as I know does not sort files but the second run seems to be in ASCII character i.e 1 10 11 2 etc.
|
Quote:
The new code is: Code:
# -*- coding: utf-8 -*- Code:
runfile('C:/Users/HP/.spyder-py3/a_.py', wdir='C:/Users/HP/.spyder-py3') |
Quote:
Now, the program below again makes the error, though not becomes the second file to be picked up, this time; as shown below in the output. Code:
# -*- coding: utf-8 -*- Code:
runfile('C:/Users/HP/.spyder-py3/a.py', wdir='C:/Users/HP/.spyder-py3') |
The files in your post #9 are sorted by ASCII character string not numerical order. In the code above you are trying to rename files based on numerical sorting.
|
Quote:
Also, is there a way to have consistent ordering in both the posts? |
According to the documentation the list output is in arbitrary order. I believe there is a module called natsort that will sort files in numerical order versus alphanumeric (ASCII).
|
Quote:
------------------- Request to please illustrate, if possible, the use of the same in my program. ------------------- I tried using the solution here, but could not understand the implementation, as it seems to use many variables as: dirName, subdirList, fileList; which am confused about. If you feel my request is premature, then please hint for the same, and would do more homework as to the meaning of these variables, which I feel are introduced due to walk(). Code:
for dirName, subdirList, fileList in os.walk(folder): |
Code:
import os Quote:
Code:
1.2.png |
Quote:
Code:
# -*- coding: utf-8 -*- Code:
runfile('C:/Users/HP/a.py', wdir='C:/Users/HP') Seems there is no way to install natsort on spyder, as got this impression after extensive googling. On restarting kernel, and applying other means, finally got to the below command:, which also didn't work: Code:
install natsort Code:
--------------------------------------------------------------------------- But, even these results seem not to help. |
You might be able to install it via pip but I don't know if spyder will automatically know it exists. Regardless of sorting your original code was just renaming files without really knowing what you were actually renaming. From the code it seems like all you want to do is replace the first "." with "_". What don't you use string replace and search for a "1." (to pickup the first "." and replace it with a "1_"
|
Quote:
Please tell if should make a separate post about the installation of natsort, on Anaconda? |
Yes, you can create a new thread on natsort module.
However, looking at it from a new angle.. Code:
import os Quote:
Code:
old file name: 1.2.png |
Look. Honestly, you should solve this by renaming the images. You need to zero-pad the numbers. Assuming the numbers go into the dozens, 1_1.png would need to be renamed to 1_01.png. If they go into the hundreds, then it becomes 1_001.png.
If you can’t just save out another image sequence with the proper padded numbering, then you can use Python to do the renaming. I can give you help with that, if you need it. |
All times are GMT -5. The time now is 11:11 AM. |