Table of Contents
This practical guide explains how to batch rename files in python with clear steps and useful context. It also covers common requirements, potential problems, and the details that can help you get better results.
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.
Conclusion
Understanding Python makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.
FAQ
How do you batch rename files in python?
Follow the steps in this guide in order, confirm the required settings or tools, and verify the result before making additional changes.
Is it safe to batch rename files in python?
It is generally safe when you follow the recommended instructions, use trusted tools, and avoid changing settings you do not understand.
What should you do if the process does not work?
Recheck the requirements, restart the device or application when appropriate, and review each step for missed settings or compatibility issues.
Reader Comments 0
Sign in with email or Google to join the discussion.