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
Click For Summary
SUMMARY

This discussion focuses on renaming a series of .tif files numbered from 18533 to 18612 using a Python script. The initial code provided by the user failed to increment the index correctly, resulting in all files being renamed to the same name. The solution involved adding an increment statement (i += 1) to ensure unique names. Additionally, participants emphasized the importance of sorting the file list obtained from the glob module before renaming to maintain numeric order and suggested creating backups to prevent data loss.

PREREQUISITES
  • Familiarity with Python 3.0 and the os module
  • Understanding of the glob module for file pattern matching
  • Basic knowledge of file handling and renaming in Python
  • Awareness of data backup practices to prevent data loss
NEXT STEPS
  • Learn how to use Python's glob module effectively for file management
  • Research sorting techniques in Python, specifically using .sort() and sorted()
  • Explore best practices for data backup before executing file manipulation scripts
  • Investigate methods for preserving file metadata during renaming or copying
USEFUL FOR

This discussion is beneficial for Python developers, data managers, and anyone involved in file organization and manipulation, particularly those working with image files and requiring safe renaming practices.

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?
 
  • 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
 
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   Reactions: 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   Reactions: 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   Reactions: Nugatory, FactChecker and jedishrfu

Similar threads

  • · Replies 9 ·
Replies
9
Views
1K
Replies
2
Views
3K
Replies
7
Views
2K