Rename 500 photos in a second with Python
Phones and cameras name files like IMG_20261001_4821.jpg. Renaming hundreds of them by hand takes most of an evening.
This script creates a folder of 500 camera-style files, then renames every one to a readable name with the trip name, the date and a running number. Change the trip name below and press Run. It really creates and renames the files, inside your browser's private file system.
IMG_20261001_4821.jpg → Goa_2026-10-01_001.jpg, for every file at once.
The Python behind it · 25 lines
1import os, time23trip = "Goa" # what to call the photos4trip = trip.replace("/", "-").replace("\\", "-").strip() or "Trip"56# Make a folder of 500 camera files, just like your phone dumps them7folder = "/tmp/phone-photos"8os.makedirs(folder, exist_ok=True)9for f in os.listdir(folder):10 os.remove(os.path.join(folder, f))11for i in range(500):12 open(f"{folder}/IMG_{20261001 + i // 50}_{4821 + i * 7}.jpg", "w").close()1314print("Before:", sorted(os.listdir(folder))[:3], "...")1516start = time.perf_counter()17for n, name in enumerate(sorted(os.listdir(folder)), start=1):18 day = name[4:12] # the date is inside the camera's file name19 new = f"{trip}_{day[:4]}-{day[4:6]}-{day[6:]}_{n:03}.jpg"20 os.rename(f"{folder}/{name}", f"{folder}/{new}")21took = (time.perf_counter() - start) * 10002223print("After: ", sorted(os.listdir(folder))[:3], "...")24print(f"\n✓ Renamed {len(os.listdir(folder))} photos in {took:.0f} ms")25print("By hand at 5 seconds each, that's about 42 minutes.")
How the code works, step by step
1.Make the name safe
A / in a file name would point to a folder that does not exist, so it is swapped for a dash. The or "Trip" gives a fallback when the box is empty.
3trip = "Goa" # what to call the photos4trip = trip.replace("/", "-").replace("\\", "-").strip() or "Trip"2.Create 500 test files
os.makedirs creates the folder, the first loop clears anything left from an earlier run, and the second creates 500 empty files with camera-style names: 50 per day over 10 days.
6# Make a folder of 500 camera files, just like your phone dumps them7folder = "/tmp/phone-photos"8os.makedirs(folder, exist_ok=True)9for f in os.listdir(folder):10 os.remove(os.path.join(folder, f))11for i in range(500):12 open(f"{folder}/IMG_{20261001 + i // 50}_{4821 + i * 7}.jpg", "w").close()3.Rename every file
enumerate counts the files from 1. Slicing name[4:12] pulls the date out of the camera's name, the f-string builds the new name with a zero-padded number ({n:03} gives 001), and os.rename does the rename. time.perf_counter measures how long it took.
16start = time.perf_counter()17for n, name in enumerate(sorted(os.listdir(folder)), start=1):18 day = name[4:12] # the date is inside the camera's file name19 new = f"{trip}_{day[:4]}-{day[4:6]}-{day[6:]}_{n:03}.jpg"20 os.rename(f"{folder}/{name}", f"{folder}/{new}")21took = (time.perf_counter() - start) * 10004.Show before and after
Listing the folder again proves the rename happened. The last line compares that with doing it by hand at about 5 seconds a file.
23print("After: ", sorted(os.listdir(folder))[:3], "...")24print(f"\n✓ Renamed {len(os.listdir(folder))} photos in {took:.0f} ms")25print("By hand at 5 seconds each, that's about 42 minutes.")
Make it yours
- On your own computer, point folder at a real folder of photos (try it on a copy first).
- Read the real date a photo was taken from its EXIF data with the Pillow library.
- Sort files into one folder per month instead of renaming them.
Questions
- Will this rename files on my computer?
- Not here. In the browser, Python runs in a private, temporary file system, so it only touches the 500 test files it creates. To rename your own files, run the same code with Python installed on your computer.
- What if two files end up with the same new name?
- The running number at the end ({n:03}) makes every new name unique, so os.rename never overwrites a file.