LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2024, 08:04 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
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'

Last edited by ajiten; 04-07-2024 at 08:06 AM.
 
Old 04-07-2024, 09:09 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by ajiten View Post
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
 
Old 04-07-2024, 09:37 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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.
 
Old 04-08-2024, 01:04 AM   #4
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
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.

Last edited by ajiten; 04-08-2024 at 03:47 AM.
 
Old 04-08-2024, 02:10 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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??
 
Old 04-08-2024, 02:15 AM   #6
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by grail View Post
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.
 
Old 04-08-2024, 02:19 AM   #7
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by michaelk View Post
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.

Last edited by ajiten; 04-08-2024 at 02:39 AM.
 
Old 04-08-2024, 03:55 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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.
 
Old 04-08-2024, 04:05 AM   #9
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by michaelk View Post
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

Last edited by ajiten; 04-08-2024 at 04:10 AM.
 
Old 04-08-2024, 04:38 AM   #10
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by michaelk View Post
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
 
Old 04-08-2024, 04:40 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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.

Last edited by michaelk; 04-08-2024 at 04:48 AM.
 
Old 04-08-2024, 04:54 AM   #12
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by michaelk View Post
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?

Last edited by ajiten; 04-08-2024 at 04:56 AM.
 
Old 04-08-2024, 05:54 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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).
 
Old 04-08-2024, 09:12 AM   #14
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by michaelk View Post
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

Last edited by ajiten; 04-08-2024 at 09:39 AM.
 
Old 04-08-2024, 03:06 PM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
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']
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Converting your Python 2 code to Python 3 LXer Syndicated Linux News 0 03-28-2020 10:03 PM
[SOLVED] NEED HELP! Want to code PERL and Python on VS Code on Fedora KDE 27 GHOSTIN7HESHELL Linux - Software 13 02-15-2018 05:00 PM
[SOLVED] Learning Python, how should I implement the following problem in Python 3 code? rblampain Programming 8 05-29-2016 10:53 AM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration