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 |
All times are GMT -5. The time now is 11:51 AM. |