The Problem

You have a folder with 200 photos named IMG_0001.jpg, IMG_0002.jpg, and so on. You want them renamed to vacation-01.jpg, vacation-02.jpg... Doing this by hand would take forever. Python can do it in under a second.

What You'll Learn

  • How to write and run a Python script
  • How to loop through files in a folder
  • How to rename files safely

Step 1: Set Up Python

First, check if Python is installed. Open your terminal and type:

$ python3 --version
Python 3.12.3

If you get "command not found," install Python from python.org.

Step 2: The Script

Create a new file called rename.py:

import os

folder = "/path/to/your/photos"   # CHANGE THIS
new_name = "vacation"              # CHANGE THIS

files = os.listdir(folder)
files.sort()

for i, old_name in enumerate(files, start=1):
    ext = old_name.split(".")[-1]           # get file extension
    new = f"{new_name}-{i:02d}.{ext}"       # e.g. vacation-01.jpg
    old_path = os.path.join(folder, old_name)
    new_path = os.path.join(folder, new)
    os.rename(old_path, new_path)
    print(f"Renamed: {old_name} → {new}")

print("Done!")

Step 3: Test First!

Always test with a copy of your files. Never run a rename script on your original photos without testing.

# Make a test folder
$ mkdir ~/test-rename
$ cp ~/Photos/*.jpg ~/test-rename/

# Run the script on the test folder
$ python3 rename.py

Step 4: Run It for Real

Once you're confident it works on the test folder, change the folder variable to your actual photo folder and run it again.

How It Works (Line by Line)

  • os.listdir(folder) — gets a list of all files in the folder
  • files.sort() — sorts them alphabetically so they're numbered in order
  • enumerate(files, start=1) — gives each file a number starting at 1
  • split(".")[-1] — grabs the file extension (jpg, png, etc.)
  • {i:02d} — formats the number as 01, 02, 03... (always two digits)
  • os.rename() — does the actual renaming

Common Errors and Fixes

"FileNotFoundError": Double-check the folder path. Use pwd to see where you are.

"PermissionError": You don't have permission to modify those files. Try running in a folder you own.

Files renamed in wrong order: Add files.sort() before the loop.

Variations

Want to add a date prefix?

from datetime import date
prefix = date.today().strftime("%Y-%m-%d")
new = f"{prefix}-{new_name}-{i:02d}.{ext}"
# Result: 2026-08-06-vacation-01.jpg

Next Steps

Now that you can rename files, try these next: - Rename only .jpg files and skip everything else - Add an "undo" feature that reverts the renames - Read the new names from a text file