Rename 100+ Files in Seconds Using Python!


🧠 Introduction
Ever opened a folder full of files with names like IMG_001
, doc (1).pdf
, or Untitled.txt
and thought... “There has to be a faster way”?
With just a few lines of Python, you can automatically rename every file in a folder — no clicking, no dragging, and no stress.
Whether you're organizing images, PDFs, or project files, Python makes it simple and fast.
🛠️ What You’ll Learn
In this tutorial, you’ll learn:
✅ How to list files in a folder using
os.listdir()
✅ How to rename files with a consistent pattern
✅ How to test safely before touching your real files
This is one of the easiest and most powerful Python automations for beginners.
💻 Full Python Code
pythonCopyEditimport os
# 🔧 Change this to your actual folder path
folder_path = "./your-folder-name" # Example: "./images" or r"C:\Users\You\Desktop\rename_test"
# 🔁 Loop through all files in the folder
for i, filename in enumerate(os.listdir(folder_path)):
old_path = os.path.join(folder_path, filename)
new_name = f"file_{i}.txt" # Change the pattern if you like
new_path = os.path.join(folder_path, new_name)
os.rename(old_path, new_path)
print("✅ All files renamed successfully!")
🔍 How It Works (Line by Line)
Let’s break this down:
pythonCopyEditimport os
Imports Python's built-in os module — it lets you interact with your file system (folders, paths, files).
pythonCopyEditfolder_path = "./your-folder-name"
Replace "./your-folder-name"
with the path to your folder.
On Windows, use r"C:\Users\You\Desktop\folder"
to avoid backslash errors.
pythonCopyEditfor i, filename in enumerate(os.listdir(folder_path)):
os.listdir()
lists all files in the folderenumerate()
gives each file an index (0, 1, 2…)
pythonCopyEdit old_path = os.path.join(folder_path, filename)
new_name = f"file_{i}.txt"
new_path = os.path.join(folder_path, new_name)
These lines:
Get the full original path
Build a new name like
file_0.txt
Build the full path for the renamed file
pythonCopyEdit os.rename(old_path, new_path)
This line actually renames the file 💥
🧪 Example Before & After
Original files:
CopyEditIMG_101.jpg
IMG_102.jpg
IMG_103.jpg
After script runs:
CopyEditfile_0.txt
file_1.txt
file_2.txt
✅ You can change .txt
to .jpg
, .pdf
, or keep the original extension (we’ll cover that in a future post).
🛡️ Safety Tip
Always test this in a copy of your folder first. Renaming is permanent unless you back it up.
🚀 Run This on Your Own Computer
Install Python from python.org
Save the script as
rename_files.py
Open a terminal → go to the folder where your script is saved
Run it with:
nginxCopyEditpython rename_files.py
🔁 Done in seconds!
🎓 Takeaway
You just automated something that would take forever to do by hand.
This is just the start — Python can rename, organize, filter, move, and even clean your files.
Subscribe to my newsletter
Read articles from Eugene Cul directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
