How to batch rename files in Python
You can automate bulk renaming with a simple Python script. Pass a template into the Python script and allow it to rename all files in a directory using a consistent naming convention.
Once the Python script is ready, you can run it on the command line and the script will rename all the files in a specified directory.
How to iterate through all files in a directory
There are many ways you can batch rename files in Windows, including through Command Prompt or Windows File Explorer.
Another way you can rename files is by using a Python script. If you're not familiar with Python, there are ways you can learn to write better Python code.
To rename all the files in a given directory, you'll need to iterate over the set of files. You can see the full example in this GitHub repo.
1. Create a new folder to store the files you want to rename:
2. Create a new file named batch-rename.py.
3. At the top of the file, import the os module. This will allow you to access the operating system's files and folders:
import os
4. Specify the directory where you have stored the files:
dir_path = "C:UsersSharlDesktopfiles"
You can also use relative paths instead. For example, if your script and file folder are in the same directory, your file path might look like this:
dir_path = "files"
5. Initialize a counter that you will use to append a counter to the end of the filename:
counter = 1
6. Add a for loop to iterate through each file in the directory:
for filename in os.listdir(dir_path): print("Renaming: " + filename + ".")
7. To test the script, run it on the command line using the python command. Make sure you navigate to the folder location where you stored your script:
cd Desktop python batch-rename.py
How to rename all files based on a certain pattern
The user will need to enter a template into the script, such as "Financial_Planning". The script will rename all files to the pattern provided and add a count to the end of the filename.
1. At the top of the file, import the sys module.
import sys
This will allow you to accept command line arguments. When running the script on the command line, you can enter the pattern you want to use to rename your file.
python batch-rename.py "Financial_Planning"
2. After the input statements, enter the command line arguments. Otherwise, an error message will appear:
commandLineArgs = sys.argv if len(commandLineArgs) > 1: pattern = commandLineArgs[1] + "_{}" else: print('Enter a pattern for the new filenames as a command line argument') sys.exit()
3. Inside the for loop, when iterating through each file, get its file extension:
file_ext = os.path.splitext(filename)[1]
4. Create a new file name based on the given template. Add a number to the end of the file name and add the file extension again:
new_filename = pattern.format(counter) + file_ext
5. Rename the file with the new filename:
oldFileName = os.path.join(dir_path, filename) newFileName = os.path.join(dir_path, new_filename) os.rename(oldFileName, newFileName)
6. Increment the counter for the next file:
counter += 1
7. After the for loop, the confirmation message will appear:
print("All files renamed.")
8. On a command line, navigate to the directory where you stored your Python script. Use the python command followed by the pattern to rename your file:
9. Open file explorer and view the renamed files:
Python scripts are a very useful way to automate simple tasks, such as renaming multiple files at once. You can explore other ways to batch rename files on your computer.
You should read it
- Manage files and folders in Python
- How to batch rename files on Windows without software
- How to batch rename files on Mac
- Working with File in Python
- 5 software to rename files in batch
- More than 100 Python exercises have solutions (sample code)
- How to Launch Python Files Using Windows Command Prompt
- Bookmark 5 best Python programming learning websites
- 6 ways to rename files and folders in Windows 10
- For in Python loop
- Multiple choice quiz about Python - Part 3
- 5 choose the best Python IDE for you
Maybe you are interested
How to Convert Files and Folders to ISO on Windows
How to use Motrix Download Manager to download Windows files
Fix GarageBand not showing audio files quickly
How to print multiple Word files at once on your computer
Windows 11 is about to support content search in local video and audio files
Scammers are using fake Windows updates to steal users' files