Renaming files

  • Python
  • Thread starter member 428835
  • Start date
  • Tags
    files
  • #1

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)
 

Answers and Replies

  • #2
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?
 
  • #3
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
  • #4
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
  • #5
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?
 
  • #6
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
  • #7
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
  • #8
Excellent points all around! I can't even imagine saving over data incorrectly
 
  • #9
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
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>.
 
  • #12
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.
 
  • #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

Suggested for: Renaming files

Replies
4
Views
463
Replies
12
Views
4K
Replies
57
Views
2K
Replies
4
Views
503
Replies
9
Views
584
Replies
16
Views
2K
Back
Top