๐ง Bulk Renaming Files Using Python: A Simple Guide
Have you ever been stuck with hundreds of files named like IMG_001.jpg
, IMG_002.jpg
, and thought—“There has to be a better way!” Well, there is—and Python makes it ridiculously easy.
In this blog, we'll explore how to bulk rename files using just a few lines of Python. Whether you're organizing photos, reformatting project files, or cleaning up a download folder, this trick will save you time and sanity.
๐ ️ What You’ll Need
-
Basic knowledge of Python
-
Python installed on your system
-
A folder of files that need renaming
We’ll be using the os
and pathlib
modules, which come built-in with Python—no need to install anything extra.
๐ Step 1: Understanding the Folder Structure
Let’s say you have a folder called images/
with files named like:
IMG_001.jpg
IMG_002.jpg
IMG_003.jpg
And you want to rename them to:
Vacation_001.jpg
Vacation_002.jpg
Vacation_003.jpg
๐งพ Step 2: The Python Script
Here's a basic script to get the job done:
import os
folder_path = 'images' # Change this to your folder
new_prefix = 'Vacation_'
for count, filename in enumerate(os.listdir(folder_path), start=1):
file_ext = os.path.splitext(filename)[1]
new_name = f"{new_prefix}{count:03}{file_ext}"
src = os.path.join(folder_path, filename)
dst = os.path.join(folder_path, new_name)
os.rename(src, dst)
print(f"Renamed: {filename} -> {new_name}")
๐ What’s Going On Here?
-
enumerate()
gives us both the index and the filename. -
os.path.splitext()
splits the filename from its extension. -
count:03
ensures numbering like001
,002
, etc. -
os.rename()
renames the file. -
The script prints each change, so you can track what’s happening.
✅ Bonus: Filter by File Type
Want to rename only .jpg
files and skip the rest? Just add a quick check:
if filename.endswith('.jpg'):
# Proceed with renaming
๐จ Safety Tip: Test First
Before running on your actual files, test this on a copy of your folder. Renaming is irreversible unless you write a rollback script.
๐ง Use Cases
-
Renaming image batches for consistency
-
Reformatting datasets for machine learning
-
Organizing downloaded media
-
Cleaning up messy folders from clients or colleagues
๐ฆ Wrap-Up
Python makes file renaming feel like beautiful. Once you get the hang of it, you’ll start thinking of all kinds of automation possibilities. This is just one small way Python can save you hours of manual clicking.