Python Renaming File Numbers from 18533 to 18612 in Order - Python Script Tutorial

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    files
AI Thread Summary
A user seeks assistance with a Python script to rename .tif files sequentially from 18533 to 18612, but initially encounters an issue where all files are renamed to the same name. The solution involves adding an incrementing variable to ensure unique names. The discussion highlights the use of the `glob` module for file retrieval, noting that it may not guarantee a sorted list across platforms. Participants emphasize the importance of backing up data before running such scripts to avoid data loss and suggest methods for preserving file creation dates during renaming. The conversation also touches on the complexities of renaming files when dealing with existing names in the target directory.
member 428835
Hi PF!

I have a folder that has several .tif files, each with a number from 18533 through 18612. I'd like to rename these numbers in order such that 18533 --> 01,...,18612 --> 79. I've tried with the following code, but it's renaming all the files to just one. Any help would be awesome!

Python:
import os
import glob
import re

src = '/Users/joshmccraney/Desktop/LF_2/'
ext = '.tif'
i = 0
for filename in glob.glob(os.path.join(src,'*' + ext)):
    newname = os.path.join(src,'image-{:02d}{}'.format(i,ext))
    print('renaming "%s" to "%s"...' % (filename, newname))
    os.rename(filename,newname)
 
Technology news on Phys.org
OOOOOOF, all i needed to do was include i += 1 on a new line 12. Feels dumb, but thanks!

That being said, can someone explain to me what glob does in this instance?
 
On unix using *.tif on a command would be expanded to a list of all tifs in the current directory. The shell did it.

On windows, the command shell doesn't do this expansion and each application would need the use glob() to get the list of files.

https://docs.python.org/3/library/glob.html

According to this stackexchange discussion, glob is short for global.

https://stackoverflow.com/questions/58168701/why-is-glob-called-glob-what-is-the-full-form-of-it
 
  • Like
Likes member 428835
One thing about your code when testing it you should make a copy of the directory in case it fails horribly.

Also i don't think glob is guaranteed to return a sorted list of files on all platforms.

Another trick is to have your program write a script of mv commands that do the changes and then after inspection you can run it.
 
  • Like
Likes FactChecker and member 428835
jedishrfu said:
One thing about your code when testing it you should make a copy of the directory in case it fails horribly.

Also i don't think glob is guaranteed to return a sorted list of files on all platforms.

Another trick is to have your program write a script of mv commands that do the changes and then after inspection you can run it.
Yea, turns out I've renamed all the files but the order is incorrect. And I made a copy beforehand because I knew I'd fail hahahah

I'm on a mac: these commands seem to work, but dang I don't know how to preserve numeric order. Any help?
 
First use glob to get your list then sort the list before iterating over ot to do the rename.

Glob may have an option to return a sorted list but i can't remember.
 
  • Like
Likes member 428835
jedishrfu said:
One thing about your code when testing it you should make a copy of the directory in case it fails horribly.

Also i don't think glob is guaranteed to return a sorted list of files on all platforms.

Another trick is to have your program write a script of mv commands that do the changes and then after inspection you can run it.
YES! I can not emphasize enough how important it is to be very careful with scripts like this. It is possible to wipe out massive amounts of critical data. This is another situation where well-tested backup software can pay off. (Don't ask me how I know.)
 
  • Like
Likes member 428835
Excellent points all around! I can't even imagine saving over data incorrectly
 
Be patient. With Saving done often enough you won't need to imagine!:nb)
 
  • #10
Another usecase that can affect your script:

Suppose you generalize the script to take any set of sequenced files and then resequence them starting at a user chosen starting index.

You have files 001.dat thru 100.dat and you want to resequence them to start at 010. The problem you will see is 001.dat can’t be renamed to 010.dat since 010.dat already exists.

One way past this issue is to copy the files to a new directory instead so the originals remain intact and renamed files are in the new directory.

One last thing to consider is preservation of the create time. For a move I think it’s preserved inherently but for a copy it will get todays date and so your script might have to adjust it on the copy.
 
  • #11
jedishrfu said:
First use glob to get your list then sort the list before iterating over ot to do the rename.

Glob may have an option to return a sorted list but i can't remember.
In python you can just call <list>.sort() to sort it before iterating, or iterate over sorted(<list>.
 
  • Like
Likes jedishrfu
  • #12
jedishrfu said:
Another usecase that can affect your script:

Suppose you generalize the script to take any set of sequenced files and then resequence them starting at a user chosen starting index.

You have files 001.dat thru 100.dat and you want to resequence them to start at 010. The problem you will see is 001.dat can’t be renamed to 010.dat since 010.dat already exists.

If increasing the index, start with the largest index.
If decreasing the index, start with the smallest index.
 
  • Like
Likes jedishrfu
  • #13
Given the potential to make a mess, I wouldn't write a script. I'd write a script to write a script, and carefully examine that script before running it.
 
Last edited:
  • Like
Likes Nugatory, FactChecker and jedishrfu
Back
Top