ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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.
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
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 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.
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?
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).
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).
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.