Batch Rename Files with Python: Rename a Thousand Files in Seconds

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:

import os import glob # Settings folder = os.path.expanduser("~/Pictures/Trip") # change to your folder pattern = "*.jpg" # which files to pick prefix = "Trip_" # new filename prefix DRY_RUN = True # True=preview False=rename files = sorted(glob.glob(os.path.join(folder, pattern))) for i, path in enumerate(files, start=1): folder_part, old_name = os.path.split(path) ext = os.path.splitext(old_name)[1] # keep the extension new_name = f"{prefix}{i:03d}{ext}" # Trip_001.jpg new_path = os.path.join(folder_part, new_name) if DRY_RUN: print(f"Preview: {old_name} -> {new_name}") else: os.rename(path, new_path) print(f"Renamed: {old_name} -> {new_name}") print() print(f"Processed {len(files)} files")

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

new_name = old_name.replace("IMG_", "")

For example, IMG_2381.jpg becomes 2381.jpg.

Rule 3: Add a prefix / suffix

new_name = "20260909_" + old_name # add a prefix new_name = old_name.replace(".jpg", "_final.jpg") # add a suffix

Run it

In Command Prompt, navigate to where you saved rename.py, then run:

python rename.py

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:

if os.path.exists(new_path): print(f"Skipping: {new_name} already exists") continue os.rename(path, new_path)

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:

counter = 1 for folder_part, dirs, filenames in os.walk(folder): for old_name in sorted(filenames): if not old_name.endswith(".jpg"): continue new_name = f"{prefix}{counter:03d}.jpg" os.rename(os.path.join(folder_part, old_name), os.path.join(folder_part, new_name)) counter += 1

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:

import shutil shutil.copytree(folder, folder + "_backup")

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.