How to Automate File Renaming with Python (and Never Do It by Hand Again)

If you’ve ever found yourself renaming dozens of files one by one—’report_final_v2.pdf’, ‘report_FINAL_v3.pdf’—you know the pain. Renaming is a classic busywork task that eats up minutes here and there, but across a year, it adds up to hours lost. The good news: you can automate it with a tiny Python script in your toolkit. Here’s how.

First, let’s understand the core problem. You have a directory full of files with inconsistent names, and you want to apply a consistent pattern. Maybe you want to add the date to the front, replace spaces with underscores, or convert everything to lowercase. The script needs to be flexible enough to handle these cases without breaking.

The basic approach is to use the os module to list files, then apply a transformation function to each filename. For example, a simple script to convert filenames to lowercase and replace spaces looks like this:

import os
for filename in os.listdir(‘.’):
new_name = filename.lower().replace(‘ ‘, ‘_’)
os.rename(filename, new_name)

That’s it. Run it from the command line, and every file in the current directory is renamed in a fraction of a second. But wait—what if you make a mistake? That’s why you should always add a dry-run mode first. Print the changes to the console without actually renaming, so you can review before committing. Add a flag like –dry-run and only execute os.rename when the flag is absent.

Now, real-world filenames have more variations. You might have patterns like ‘report_2023-01-01.xlsx’ and you want to extract the date and reformat it. The regex module (re) becomes your friend. For example, you can use a pattern like re.search(r'(d{4})-(d{2})-(d{2})’, filename) to pull out parts of the date, then reconstruct the name as ‘2023_01_01_report.xlsx’. The possibilities are endless.

A common use case is cleaning up downloaded files. If you download invoices from a portal, they often come with names like ‘invoice_12345.pdf’ and you want to rename them to ‘CompanyName_YYYY-MM-DD.pdf’. You can build a mapping from invoice numbers to company names in a JSON file, and the script looks it up. That’s what I did for my own accounting—saves me 20 minutes every month.

One more tip: always handle edge cases. Files might have no extension, or you might accidentally overwrite an existing file. Use os.path.exists() to check for collisions, and have a strategy—either append a number or skip. A robust script should never lose data.

If you don’t want to write your own from scratch, I’ve packaged a ready-to-use script in the library. It supports regex patterns, dry-run, and collision handling out of the box. You can customize it with a simple config file. But even if you roll your own, the time investment pays off the first time you run it.

Automating file renaming is just the tip of the iceberg. Once you get comfortable with Python, you’ll start seeing other repetitive tasks that can be scripted—moving files, compressing folders, even sending emails. The key is to start small and build up your toolkit. In the next post, I’ll cover how to build a similar script for bulk PDF merging, which is another classic pain point.