Phone photos, scanned documents and files from colleagues pile up into a mess of IMG_2381.jpg, Untitled(3).pdf and scan_20260115_144732.png. When you have a thousand files, you're not going to rename them by hand. This tutorial shows you how to tidy an entire folder's filenames in one command with a dozen lines of Python — add prefixes, number them sequentially, strip out junk text, all automatically.
Where does this come up?
Renaming isn't just about tidiness — it saves time and prevents mistakes:
- Photos — turn IMG_2381.jpg into Tokyo_001.jpg so you can find them at a glance
- Invoices / contracts — add a date prefix like 20260909_invoice_xxx so they sort correctly
- Reports — tag monthly files with "_final" so old and new versions never get mixed up
Two tools to know first
The whole script relies on just two built-in Python modules:
- glob — finds files matching a wildcard, e.g. *.jpg means "all jpgs"
- os.rename — the core command that swaps the old name for the new one
Both are built in, so there's nothing to pip install.
The full script
Create a new file called rename.py and paste in the code below:
Line by line
- sorted(glob.glob(...)) — finds every .jpg in the folder and sorts them alphabetically, so the numbering order stays stable.
- os.path.split(path) — splits the path into (folder, filename); we only change the filename part.
- os.path.splitext(old_name)[1] — extracts the extension (e.g. .jpg), so we can add it back unchanged.
- f"{prefix}{i:03d}{ext}" — builds the new name. :03d means "three digits, zero-padded", so the 7th file becomes Trip_007.jpg.
- DRY_RUN — a safety switch. Start with True to preview without changing anything; only flip it to False when it looks right.
Three common renaming rules
Changing the new_name line swaps the renaming rule. Three of the most useful:
Rule 1: Sequential numbering (shown above)
Trip_001.jpg, Trip_002.jpg… perfect for photos and batch documents.
Rule 2: Strip fixed text
For example, IMG_2381.jpg becomes 2381.jpg.
Rule 3: Add a prefix / suffix
Run it
In Command Prompt, navigate to where you saved rename.py, then run:
Run it once with DRY_RUN = True to preview, and only flip to False once it looks correct. In seconds, the whole folder is tidy.
Watch out for name clashes
If two different files end up with the same name, os.rename will silently overwrite the old one and your data is gone. Add a "skip on clash" guard before running for real:
Place this right before os.rename — it skips any duplicate name, which is much safer.
Going further: rename subfolders too
The script above only touches the top level. To include every subfolder, swap glob for os.walk:
os.walk automatically walks through every subfolder, which is ideal for tidying a whole project folder in one go.
Safety first: back up
Renaming is irreversible, so the safest approach is to copy a small test folder first and only use it on the real files once it looks right. You can even back up the whole folder with shutil.copytree:
If you rename something wrong, restore from the backup and you lose nothing.
Summary
This Python tutorial summary: Batch renaming is one of the highest value-for-effort automation projects to start with — no packages to install, and a dozen lines of code solve a chore you face every day. Once you're comfortable, you can apply the same pattern to batch-renaming PDFs, batch compression, or even batch watermarking. Automation often starts with exactly these small wins.