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

  • Python
  • Thread starter member 428835
  • Start date
  • Tags
    files
In summary, the conversation discusses an issue with renaming multiple .tif files in a specific order using Python. The code provided is renaming all the files to just one, and the help of using glob is suggested. Glob is short for global and is used to get a list of files in a directory. It is important to be cautious when using scripts to avoid accidentally deleting or overwriting data. Suggestions are made to use glob to get a sorted list of files before iterating over them to do the renaming. It is also mentioned to be aware of preserving the create time when copying files. Finally, it is suggested to write a script to write a script to avoid causing any issues.
  • #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)
 
Technology news on Phys.org
  • #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
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?
 
  • #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
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
  • #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
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

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

How can I rename a range of file numbers in sequential order using a Python script?

To rename a range of file numbers from 18533 to 18612 in order using a Python script, you can use the os.rename() function. This function takes two arguments: the current file name and the new file name. You can use a for loop to iterate through the range of file numbers and use string concatenation to create the new file name with the incremented number.

Do I need any prior knowledge of Python to follow this tutorial?

Yes, basic knowledge of Python is required to follow this tutorial. You should be familiar with concepts such as variables, loops, and string manipulation. However, the tutorial provides step-by-step instructions and explanations that can help beginners understand the code.

Can this tutorial be used for renaming files with different file extensions?

Yes, this tutorial can be used for renaming files with different file extensions. As long as the file names follow a similar pattern, you can use the same logic to rename them using a Python script.

Is there an alternative method to rename file numbers besides using a Python script?

Yes, there are several alternative methods to rename file numbers. You can use a batch file or a third-party software specifically designed for batch renaming files. However, using a Python script gives you more control and flexibility in customizing the renaming process.

Can this tutorial be applied to other programming languages besides Python?

Yes, the logic and concepts used in this tutorial can be applied to other programming languages as well. However, the syntax and functions used may differ depending on the language. It is recommended to consult the documentation of the programming language of your choice for the specific functions and syntax needed for batch renaming files.

Similar threads

Back
Top