Lesson 12 - How to Rename Files in a Folder Using Python (Step-by-Step Guide)
Learn how to easily rename multiple files in a folder using Python. This beginner-friendly tutorial shows you how to automate file renaming with just a few lines of code.
PYTHON
Leonardo Gomes Guidolin
4/21/20252 min read
🚀 Why Use Python to Rename Files?
If you’ve ever needed to rename a large number of files—images, documents, or data files—you know how time-consuming it can be to do it manually.
Python is a powerful tool that can help automate this process. With just a few lines of code, you can rename hundreds of files in seconds. Whether you're working on a data science project, organizing files for a website, or managing downloads, this guide is for you.
🧰 Prerequisites
To follow this tutorial, you only need:
Python 3 installed on your system
A folder with files you want to rename
Basic knowledge of Python (optional – we explain everything!)
No need to install extra libraries!
Step-by-Step: Rename Files in a Folder Using Python
Let’s look at a basic script that renames all files in a folder using a numbered pattern like file_1.jpg, file_2.jpg, etc.
✅ Python Script:
import os
# Step 1: Define the folder path
folder_path = 'path/to/your/folder' # Change this to your actual path
# Step 2: Loop through each file in the folder
for index, filename in enumerate(os.listdir(folder_path)):
# Step 3: Get file extension (e.g., .jpg, .txt)
file_extension = os.path.splitext(filename)[1]
# Step 4: Create new filename
new_name = f"file_{index + 1}{file_extension}"
# Step 5: Get full paths
old_file = os.path.join(folder_path, filename)
new_file = os.path.join(folder_path, new_name)
# Step 6: Rename the file
os.rename(old_file, new_file)
print(f"Renamed: {filename} ➜ {new_name}")
🔄 Example Output
Let’s say your folder has the following files:
cat.jpg
dog.jpg
car.jpg
After running the script, they’ll be renamed to:
file_1.jpg
file_2.jpg
file_3.jpg
🧠 How It Works – Line by Line
Code Explanation:
import os
Imports the os module for file handling
os.listdir(folder_path)
Lists all files in the folder
os.path.splitext(filename)[1]
Gets the file extension
os.rename(old_file, new_file)
Renames each file
✨ Customizing File Names
You can change the renaming pattern to include:
🕒 Date and Time:
from datetime import datetime
date_str = datetime.now().strftime("%Y-%m-%d")
new_name = f"{date_str}_{index + 1}{file_extension}"
📂 File Prefix:
new_name = f"projectA_image_{index + 1}{file_extension}"
💡 Replace Original Name (Preserve Extension):
base_name = os.path.splitext(filename)[0]
new_name = f"{base_name}_renamed{file_extension}"
🧪 Pro Tip: Always Test First
Before running the script on important files, test it in a separate folder to avoid losing data. You can also print the proposed changes without renaming:
print(f"Will rename: {filename} ➜ {new_name}")
🛠️ Common Use Cases
Renaming downloaded files
Organizing photo galleries
Preparing datasets for machine learning
Cleaning up inconsistent file names
Automating repetitive tasks in your workflow
💬 Final Thoughts
Renaming files in bulk is just one of the many things you can automate with Python. It saves time, eliminates human error, and is super satisfying once set up.
Learning simple automation like this can significantly boost your productivity and give you full control over your data and file management.