Reputation: 1191
I have a 100 .fits files in a directory with long names (ex. spec-0355-51788-0484.fits spec-0493-51957-0157.fits, spec-0367-51997-0430.fits, spec-0771-52370-0017.fits etc...)
I am wondering if there is a loop to rename them all in a sequence of integers so that it looks like 1.fits, 2.fits, 3.fits, 4.fits, .... 100.fits
Upvotes: 1
Views: 2089
Reputation: 665
I would recommend trying this. This will rename the files by adding an index before the name of the file.
Example:
How to install.fits, Hello world tutorial.fits, .......
would rename to
1 How to install.fits, 2 Hello world tutorial.fits, ......
import os
path = 'C:/Users/username/Desktop/My files' #path of folder containing files you want to rename
i = 1
for filename in os.listdir(path):
os.rename(os.path.join(path,filename), os.path.join(path, str(i)+ " " + filename + '.fits'))
i = i + 1
If you don't like to put the original file name at all then simply remove adding filename in os.path.join
os.path.join(path, str(i)+ " " + '.fits')) #for not including original file name
Upvotes: 0
Reputation: 18638
You could try this:
import os
import glob
os.chdir("E:/")
i = 1
for old_file in glob.glob("*.fits"):
new = str(i) + ".fits"
os.renames(old_file, new)
i=i+1
Upvotes: 1