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

  • Context: Python 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    files
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
12 replies · 2K views
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)
 
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?
 
  • Like
Likes   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: member 428835
Excellent points all around! I can't even imagine saving over data incorrectly
 
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.
 
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   Reactions: jedishrfu
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   Reactions: jedishrfu
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   Reactions: Nugatory, FactChecker and jedishrfu