Python · Tools

Automate File Renaming with Python

Renaming hundreds of files by hand is the kind of boring task computers were made for. This Python script renames a whole folder in one go — clean, consistent and numbered — in seconds.

Automate file renaming with Python

The script

This renames every file in a folder to a clean pattern like vacation-001.jpg, vacation-002.jpg and so on, while keeping the original file extension.

import os





FOLDER = "files"        # folder with the files to rename


PREFIX = "vacation"     # new base name


START = 1               # starting number





files = sorted(os.listdir(FOLDER))


counter = START





for name in files:


    path = os.path.join(FOLDER, name)


    if not os.path.isfile(path):


        continue





    ext = os.path.splitext(name).lower()


    new_name = f"{PREFIX}-{counter:03d}{ext}"


    os.rename(path, os.path.join(FOLDER, new_name))


    print(f"{name} -> {new_name}")


    counter += 1





print("Done.")

How to use it

  • Put the files in a folder called files next to the script.
  • Change PREFIX to whatever base name you want.
  • {counter:03d} pads the number to 3 digits (001, 002…) so they sort correctly.

Tip: always test on a copy of your folder first. Renaming is fast — and so is renaming the wrong things!

Drowning in repetitive tasks?

If your team does the same manual work over and over, a small custom script or tool usually pays for itself in a week. Tell me what you are stuck doing by hand.

Ask us anything×