Welcome to our automation tutorial series. In previous tutorials, you learned about automating different browser tasks with python. In this tutorial, you’ll learn about how to rename a huge number of files automatically with unique IDs/numbers. Let’s Bulk File rename in Python…
Getting Started for Bulk File rename in Python
Let us consider a scenario, you have about 1000 files in a folder named randomly by default and now you have to work with those photos and it gets harder for you to recognize them with random names so you might need to rename them with numbers like “1.png”, “2.jpg”, ….., “1000.jpeg” but you cannot rename such huge number of files manually or it’ll kill your lots of time. In such cases, you can automate this task of renaming with python and run it every time you need to do a similar task.
The only package you need for this is
os the library of python which is pre-installed with pip. So you don’t need to install any external library for this task.
The steps for the task are as follows:
- Import the required libraries.
- Then you navigate to the folder where the raw files are saved.
- And then you rename the first file.
- Now you repeat step 3 until the last file in the folder.
Diving into the Code
The first and foremost task in any python program is importing required libraries or packages (if necessary). So you’ll start with the same. As mentioned earlier, for this task you require only one os package and as it’s a pre-installed library in python you’ll import it directly to your code.
Following the algorithm, you need to define two variables, one for storing the folder names where the files are stored and the other one for the numbers to be started from.
folderPath = 'E:\\Pioneer photo\\selected photos\\' #location of the folder fileSequence = 1 #This is the value to get started from, i.e., the first file will be renamed with an unique id of 1, if you want it to be some other number you can change as required.
Now you define the actual function to do the renaming work for you.
Now you will set a for loop to get each file in the directory, one after the other. At first, you get the list of files in the folder using the inbuilt function of os module, i.e., os.listdir(folderPath), this returns a list of files in the assigned variable
folderPath and then you use the for loop to loop into each file in files_list.
files_list=os.listdir(folderPath) for filename in files_list:
Now you have got the files and you can start renaming that but the file extensions may not be the same, some files may be in png, jpg or jpeg, so you’ve to save them in the same format and for that, you require their file extension.
So now you’ve also got the file extension and you can start renaming files and saving them with the same extension. For this task, you’ll use the inbuilt rename function of os module which requires two parameters:
- Initial Location: This is the current location of the file you want to rename. You’ve already assigned the location of the folder as
folderPathand the name of the file
filenamefor every loop. So the sum of these two variables should give the initial location of the file.
- New Location and Name: This is the new location and name of the file you wish to save it as. You can assign a separate folder for it or use the same
folderPathfolder and give the name “photo1”, “photo2” and give an extension. So you concatenate all these variables together as
Now the first file is renamed and you need to increase the
fileSequence by 1 so that the next file gets saved by another name.
Executing the function
You’ve already defined the function and so you need to run it now to perform the defined tasks.
Here is the complete code for this tutorial.
import os folderPath = 'E:\Pioneer photo\selected photos\\' fileSequence = 1 def job(): files_list=os.listdir(folderPath) for filename in files_list: splitted_filename=filename.split(".","-1") extension=splitted_filename[-1] os.rename(folderPath+filename, folderPath+"photo"+str(fileSequence)+str(extension)) fileSequence +=1 job()
So, now you’ve learned how to rename a huge number of files in a directory running a simple script of python. Every time you need to perform similar tasks, you can just change the value of
folderPath with the location of the folder where the files you want to rename are located and run the program.