LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Error in python code. (https://www.linuxquestions.org/questions/programming-9/error-in-python-code-4175735769/)

ajiten 04-07-2024 08:04 AM

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"
import os
path = os.chdir(input_dir)
i=0
for file in os.listdir(path):
    print(file)

Error:
Code:

runfile('C:/Users/HP/untitled0.py', wdir='C:/Users/HP')
Traceback (most recent call last):

  File ~\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\hp\untitled0.py:9
    path = os.chdir(input_dir)

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'D:\\Screenshots\x08k\\ch_1'


ntubski 04-07-2024 09:09 AM

Quote:

Originally Posted by ajiten (Post 6494628)
Am getting weird error output, from the following code, in Python, on spyder:
Code:

input_dir = "D:\Screenshots\bk\ch_1"

\b means backspace character, use "D:\\Screenshots\\bk\\ch_1" or "D:/Screenshots/bk/ch_1" instead.

https://docs.python.org/3/reference/...cape-sequences

michaelk 04-07-2024 09:37 AM

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)
Also os.chdir does not return a value.

ajiten 04-08-2024 01:04 AM

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 -*-
"""
Created on Sun Apr  7 17:49:51 2024

@author: HP
"""
input_dir = "D:/Screenshots/bk/ch_1"
import os
os.chdir(input_dir)
i=1

for file in os.listdir(path):
    filetype=file.split('.')[-1]
    print('i: ', i, 'filetype: ', filetype)
   
    old_file= input_dir+ '/'+file
    print('old_file name: ',input_dir+ '/'+file)
    new_file= input_dir+ '/'+'1_'+str(i)+'.'+filetype
    os.rename(old_file, new_file)
    i +=1
    print ('new_file name: ', new_file)

Output on the first run of the above program:
Code:

runfile('C:/Users/HP/.spyder-py3/a.py', wdir='C:/Users/HP/.spyder-py3')
D:/Screenshots/bk/ch_1/1.1.png
D:/Screenshots/bk/ch_1/1_1.png
D:/Screenshots/bk/ch_1/1.2.png
D:/Screenshots/bk/ch_1/1_2.png
D:/Screenshots/bk/ch_1/1.3.png
D:/Screenshots/bk/ch_1/1_3.png
D:/Screenshots/bk/ch_1/1.4.png
D:/Screenshots/bk/ch_1/1_4.png
D:/Screenshots/bk/ch_1/1.5.png
D:/Screenshots/bk/ch_1/1_5.png
D:/Screenshots/bk/ch_1/1.6.png
D:/Screenshots/bk/ch_1/1_6.png
D:/Screenshots/bk/ch_1/1.7.png
D:/Screenshots/bk/ch_1/1_7.png
D:/Screenshots/bk/ch_1/Screenshot (1558).png
D:/Screenshots/bk/ch_1/1_8.png
D:/Screenshots/bk/ch_1/Screenshot (1559).png
D:/Screenshots/bk/ch_1/1_9.png
D:/Screenshots/bk/ch_1/Screenshot (1560).png
D:/Screenshots/bk/ch_1/1_10.png
D:/Screenshots/bk/ch_1/Screenshot (1561).png
D:/Screenshots/bk/ch_1/1_11.png
D:/Screenshots/bk/ch_1/Screenshot (1562).png
D:/Screenshots/bk/ch_1/1_12.png
D:/Screenshots/bk/ch_1/Screenshot (1563).png
D:/Screenshots/bk/ch_1/1_13.png
D:/Screenshots/bk/ch_1/Screenshot (1564).png
D:/Screenshots/bk/ch_1/1_14.png
D:/Screenshots/bk/ch_1/Screenshot (1565).png
D:/Screenshots/bk/ch_1/1_15.png
D:/Screenshots/bk/ch_1/Screenshot (1566).png
D:/Screenshots/bk/ch_1/1_16.png
D:/Screenshots/bk/ch_1/Screenshot (1567).png
D:/Screenshots/bk/ch_1/1_17.png
D:/Screenshots/bk/ch_1/Screenshot (1568).png
D:/Screenshots/bk/ch_1/1_18.png
D:/Screenshots/bk/ch_1/Screenshot (1569).png
D:/Screenshots/bk/ch_1/1_19.png
D:/Screenshots/bk/ch_1/Screenshot (1570).png
D:/Screenshots/bk/ch_1/1_20.png
D:/Screenshots/bk/ch_1/Screenshot (1571).png
D:/Screenshots/bk/ch_1/1_21.png
D:/Screenshots/bk/ch_1/Screenshot (1572).png
D:/Screenshots/bk/ch_1/1_22.png
D:/Screenshots/bk/ch_1/Screenshot (1573).png
D:/Screenshots/bk/ch_1/1_23.png

Output on the second run of the above program:

Code:

runfile('C:/Users/HP/.spyder-py3/a.py', wdir='C:/Users/HP/.spyder-py3')
i:  1 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1_1.png
new_file name:  D:/Screenshots/bk/ch_1/1_1.png
i:  2 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1_10.png
Traceback (most recent call last):

  File D:\Program Files\Spyder\pkgs\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\hp\.spyder-py3\a.py:19
    os.rename(old_file, new_file)

FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'D:/Screenshots/bk/ch_1/1_10.png' -> 'D:/Screenshots/bk/ch_1/1_2.png'



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.

grail 04-08-2024 02:10 AM

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??

ajiten 04-08-2024 02:15 AM

Quote:

Originally Posted by grail (Post 6494779)
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??

Seems it should be a problem of picking the wrong file, and is present in Paint too; unless manually pick the correct ordering.

ajiten 04-08-2024 02:19 AM

Quote:

Originally Posted by michaelk (Post 6494653)
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)
Also os.chdir does not return a value.

Thanks, I removed the redundant variable 'path' from the stated line.

michaelk 04-08-2024 03:55 AM

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.

ajiten 04-08-2024 04:05 AM

Quote:

Originally Posted by michaelk (Post 6494795)
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.

Sorry, am unable to understand clearly. Might be my question stating the weird pickup of the file name: 1_10.png, before the file name: 1_2.png, should be shown more clearly, with a separate program.

The new code is:
Code:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  7 17:49:51 2024

@author: HP
"""
input_dir = "D:/Screenshots/bk/ch_1"
import os
path = os.chdir(input_dir)
i=1
for file in os.listdir(path):
    print('i: ', i, 'file name: ', file)
    i+=1

It gives output as:
Code:

runfile('C:/Users/HP/.spyder-py3/a_.py', wdir='C:/Users/HP/.spyder-py3')
i:  1 file name:  1_1.png
i:  2 file name:  1_10.png
i:  3 file name:  1_11.png
i:  4 file name:  1_12.png
i:  5 file name:  1_13.png
i:  6 file name:  1_14.png
i:  7 file name:  1_15.png
i:  8 file name:  1_16.png
i:  9 file name:  1_17.png
i:  10 file name:  1_18.png
i:  11 file name:  1_19.png
i:  12 file name:  1_2.png
i:  13 file name:  1_20.png
i:  14 file name:  1_21.png
i:  15 file name:  1_22.png
i:  16 file name:  1_23.png
i:  17 file name:  1_3.png
i:  18 file name:  1_4.png
i:  19 file name:  1_5.png
i:  20 file name:  1_6.png
i:  21 file name:  1_7.png
i:  22 file name:  1_8.png
i:  23 file name:  1_9.png


ajiten 04-08-2024 04:38 AM

Quote:

Originally Posted by michaelk (Post 6494795)
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.

As a better example, have changed the name of one file, that is desired to be renamed: 1_10.png, i.e.: Screenshot (1560).
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 -*-
"""
Created on Sun Apr  7 17:49:51 2024

@author: HP
"""
input_dir = "D:/Screenshots/bk/ch_1"
import os
path = os.chdir(input_dir)
print('path: ', path)
i=1

for file in os.listdir(path):
    filetype=file.split('.')[-1]
    print('i: ', i, 'filetype: ', filetype)
   
    old_file= input_dir+ '/'+file
    print('old_file name: ',input_dir+ '/'+file)
    new_file= input_dir+ '/'+'1_'+str(i)+'.'+filetype
    os.rename(old_file, new_file)
    i +=1
    print ('new_file name: ', new_file)

The output:
Code:

runfile('C:/Users/HP/.spyder-py3/a.py', wdir='C:/Users/HP/.spyder-py3')
path:  None
i:  1 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.1.png
new_file name:  D:/Screenshots/bk/ch_1/1_1.png
i:  2 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.2.png
new_file name:  D:/Screenshots/bk/ch_1/1_2.png
i:  3 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.3.png
new_file name:  D:/Screenshots/bk/ch_1/1_3.png
i:  4 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.4.png
new_file name:  D:/Screenshots/bk/ch_1/1_4.png
i:  5 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.5.png
new_file name:  D:/Screenshots/bk/ch_1/1_5.png
i:  6 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.6.png
new_file name:  D:/Screenshots/bk/ch_1/1_6.png
i:  7 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1.7.png
new_file name:  D:/Screenshots/bk/ch_1/1_7.png
i:  8 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/1_10.png
new_file name:  D:/Screenshots/bk/ch_1/1_8.png
i:  9 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1558).png
new_file name:  D:/Screenshots/bk/ch_1/1_9.png
i:  10 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1559).png
new_file name:  D:/Screenshots/bk/ch_1/1_10.png
i:  11 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1561).png
new_file name:  D:/Screenshots/bk/ch_1/1_11.png
i:  12 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1562).png
new_file name:  D:/Screenshots/bk/ch_1/1_12.png
i:  13 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1563).png
new_file name:  D:/Screenshots/bk/ch_1/1_13.png
i:  14 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1564).png
new_file name:  D:/Screenshots/bk/ch_1/1_14.png
i:  15 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1565).png
new_file name:  D:/Screenshots/bk/ch_1/1_15.png
i:  16 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1566).png
new_file name:  D:/Screenshots/bk/ch_1/1_16.png
i:  17 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1567).png
new_file name:  D:/Screenshots/bk/ch_1/1_17.png
i:  18 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1568).png
new_file name:  D:/Screenshots/bk/ch_1/1_18.png
i:  19 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1569).png
new_file name:  D:/Screenshots/bk/ch_1/1_19.png
i:  20 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1570).png
new_file name:  D:/Screenshots/bk/ch_1/1_20.png
i:  21 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1571).png
new_file name:  D:/Screenshots/bk/ch_1/1_21.png
i:  22 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1572).png
new_file name:  D:/Screenshots/bk/ch_1/1_22.png
i:  23 filetype:  png
old_file name:  D:/Screenshots/bk/ch_1/Screenshot (1573).png
new_file name:  D:/Screenshots/bk/ch_1/1_23.png


michaelk 04-08-2024 04:40 AM

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.

ajiten 04-08-2024 04:54 AM

Quote:

Originally Posted by michaelk (Post 6494802)
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.

Can you please explain why in my post #9, the files are picked in a different order than in my post #10. I.e. in post #9, the file: 1_10.png, is picked second, but in the post #10, the file: 1_10.png, is picked after the file: 1_7.png.

Also, is there a way to have consistent ordering in both the posts?

michaelk 04-08-2024 05:54 AM

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).

ajiten 04-08-2024 09:12 AM

Quote:

Originally Posted by michaelk (Post 6494810)
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).

Saw the official documentation of natsort, though could not understand the details as natsort_key(), as at: https://natsort.readthedocs.io/en/st...natsort_keygen.

-------------------

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):
if mat_file in fileList:
    input_path = dirName + r"\file.txt"
    if os.path.isfile(input_path):
        filename.append(str(dirName.strip(folder)))

natsorted(filename)
print filename


michaelk 04-08-2024 03:06 PM

Code:

import os
from natsort import natsorted

for file in os.listdir():
    print(file)

sorted_files=natsorted(os.listdir())
print(sorted_files)

files from output of ls:
Quote:

1.10.png 1.1.png 1.2.png 1.11.png 1.21.png 1.3.png 'screenshot (1573).png'
program output:
Code:

1.2.png
screenshot (1573).png
1.3.png
1.21.png
1.10.png
1.1.png
1.11.png

['1.1.png', '1.2.png', '1.3.png', '1.10.png', '1.11.png', '1.21.png', 'screenshot (1573).png']


ajiten 04-09-2024 09:38 AM

Quote:

Originally Posted by michaelk (Post 6494899)
Code:

import os
from natsort import natsorted

for file in os.listdir():
    print(file)

sorted_files=natsorted(os.listdir())
print(sorted_files)

files from output of ls:

program output:
Code:

1.2.png
screenshot (1573).png
1.3.png
1.21.png
1.10.png
1.1.png
1.11.png

['1.1.png', '1.2.png', '1.3.png', '1.10.png', '1.11.png', '1.21.png', 'screenshot (1573).png']


Modified the program, to the one below:
Code:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  7 17:49:51 2024

@author: HP
"""
input_dir = "D:/Screenshots/bk/ch_1"
import os
from natsort import natsorted
path = os.chdir(input_dir)

j=1
for file in os.listdir(path):
    print(j,':', file)
    j+=1
   
j=1   
sorted_files = natsorted(os.listdir(path))
for file in sorted_files:
    print('>', j, ':', file)
   
i=1
for file in sorted_files: #os.listdir(path):
    filetype=file.split('.')[-1]
    print(input_dir+ '/'+file)
    os.rename(input_dir+ '/'+file, input_dir+ '/'+'1.'+str(i)+'.'+filetype)
    i +=1
    print (file)

But, this gave the error:
Code:

runfile('C:/Users/HP/a.py', wdir='C:/Users/HP')
Traceback (most recent call last):

  File D:\Program Files\Spyder\pkgs\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\hp\a.py:9
    from natsort import natsorted

ModuleNotFoundError: No module named 'natsort'

So, natsort need be installed on spyder.

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
  Cell In[5], line 1
    install natsort
            ^
SyntaxError: invalid syntax

Hence, tried to run the above program on jupyter notebook, but that too gives error:
Code:

---------------------------------------------------------------------------
ModuleNotFoundError                      Traceback (most recent call last)
Cell In[1], line 2
      1 import os
----> 2 from natsort import natsorted

ModuleNotFoundError: No module named 'natsort'

Tried googling for installing natsort, on Anaconda, and got the results on googling.

But, even these results seem not to help.

michaelk 04-09-2024 09:55 AM

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_"

ajiten 04-09-2024 06:53 PM

Quote:

Originally Posted by michaelk (Post 6494997)
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_"

But, again the files are unsorted, and the same problem should occur in the part after the '_'.

Please tell if should make a separate post about the installation of natsort, on Anaconda?

michaelk 04-09-2024 07:38 PM

Yes, you can create a new thread on natsort module.

However, looking at it from a new angle..
Code:

import os

for file in os.listdir():
    x = file.split(".")
    print("old file name:",file)
    size=len(x)
    if size == 3:
      new_file=x[0]+"_"+x[1]+"."+x[2]
      print("new file name:",new_file,"\n")
    else:
      print("new file name: No Change\n")

files:
Quote:

1.10.png 1.1.png 1.2.png 1_34.png 'screenshot (1573).png'
1.11.png 1.21.png 1.3.png
output:
Code:

old file name:  1.2.png
new file name:  1_2.png

old file name:  screenshot (1573).png
new file name: No Change

old file name:  1.3.png
new file name:  1_3.png

old file name:  1.21.png
new file name:  1_21.png

old file name:  1.10.png
new file name:  1_10.png

old file name:  1_34.png
new file name: No Change

old file name:  1.1.png
new file name:  1_1.png

old file name:  1.11.png
new file name:  1_11.png

All based on the previously posted filenames and my assumptions on what you are trying to accomplish.

dugan 04-11-2024 08:48 AM

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.