Python Beginner 2 min read

Automate a real task - rename a folder of files

Turn a boring ten-minute chore into a two-second script that renames every file in a folder.

You’ve learned variables, ifs and loops. Now let’s use them on a real chore: giving a messy folder of photos tidy, numbered names like holiday-001.jpg, holiday-002.jpg and so on.

Practise on a copy

Scripts are fast, including when they’re wrong. Copy a few files into a new test folder and run the script there first.

Step 1: list the files

from pathlib import Path

folder = Path("test-photos")

for file in folder.iterdir():
    print(file.name)

Path is Python’s built-in helper for files and folders. iterdir() gives you every item in the folder, one at a time. Run this first to check it finds your files.

Step 2: do a dry run

Before renaming anything, print what you would do:

from pathlib import Path

folder = Path("test-photos")
files = sorted(f for f in folder.iterdir() if f.suffix.lower() == ".jpg")

for number, file in enumerate(files, start=1):
    new_name = f"holiday-{number:03}{file.suffix.lower()}"
    print(file.name, "->", new_name)

In plain English

Collect only the .jpg files and sort them by name. Then go through them, counting from 1. For each one, build a new name: {number:03} pads the number to three digits, so 7 becomes 007.

Step 3: rename for real

When the dry run looks right, swap the print for rename:

    file.rename(folder / new_name)

Run it, and every photo gets its new name in an instant.

What you just did

You used a variable (folder), a loop (for ... in), a decision (only .jpg files) and a built-in library (pathlib). That’s real automation, and it’s the same pattern behind scripts that sort downloads, back up files or clean up old logs.

Try it yourself

Change the script so it also handles .png files. Then make it ask for the name prefix with input() instead of always using “holiday”.

One new script in your inbox each week

A beginner-friendly article plus a tiny challenge. Unsubscribe any time.