[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Use regular expressions to rename files in Python

[This example lets you use regular expressions to rename files in Python]

Recently I needed to rewrite a bunch of files. To keep track of the ones I had already processed, I wanted to rename them so they each had OLD_ at the beginning. I could have just renamed them in a few minutes, but being a proper programming nerd I decided to spend a few hours writing a program to do it in case I ever need to do something like this again.

WARNING: This seems to work in Windows 11 but, because it uses operating system tools, your mileage may vary in penguin- or fruit-oriented operating systems.

Using the Example

Enter a folder path or click the ellipsis to select a folder. Enter a pattern to select matching files. For example, in the picture at the top of this post, the pattern is *.* to select all files.

Next, enter regular expressions to examine the file names and produce a new result. For example, in the picture at the top of this post, the From expression is ^(.*)$. The parentheses tell the regular expression engine to save whatever is inside as a capture group. The ^ and $ match the start and end of the string respectively so this captures the entire file name.

Note that the program will apply the pattern to the file's name without its complete path so the regular expressions won't affect the path.

In the picture, the To expression is OLD_\1, which produces the string OLD_ followed by the first capture group, which holds the entire file name.

For example, these two expressions would convert test.png to OLD_test.png.

Having filled in the fields, click Preview to see a list showing the files that will be converted and their new names.

WARNING: Be sure to preview the changes before you let the program start renaming files. If you rename a file accidentally, you can probably rename it back, but I make no guarantees.

In fact, even more than usual I make no guarantees that this program won't mess up your system. I think you should be safe if you use the program carefully, but I make no promises and won't accept blame if something goes wrong. If you're in doubt, just rename the files manually.

Once you're sure the file names are correct, click Rename to rename the files.

Renaming Files

The key to the program is the following rename_files function.

import os from pathlib import Path def rename_files(path, pattern, from_regex, to_regex, make_changes=False): '''Return a list of changes. If make_changes is true, make the changes.''' results = [] directory = Path(path) for file in directory.glob(pattern): # List the changes. from_name = Path(file).name to_name = re.sub(from_regex, to_regex, from_name) results.append((from_name, to_name)) # Make the changes. if make_changes: new_file = Path(file).with_name(to_name) os.rename(file, new_file) return results

This function first creates a Path object representing the selected file path. It calls that object's glob method to list the files that match the indicated pattern (like *.* or *.txt) and loops through the files.

Inside the loop, the function calls Path(file).name to get the file's name without its path. It then calls re.sub to use the regular expression module to replace the From expression with the To expression on this file name.

YET ANOTHER WARNING: Regular expressions are pretty confusing so be sure to preview the changes so you're sure the regular expression module will make the replacements you think it will.

Also note that there are several different flavors of regular expressions floating around the internet so be sure you're using the one that this module supports.

Having found the file's new name, the function adds the file's old and new names to the results list.

Next, if make_changes is True, the code uses os.rename to actually rename the file. (The Preview button calls this function with make_changes = False and the Rename button calls it with make_changes = True.)

After it has processed all of the files, the function returns the results list.

Conclusion

This program worked for me. After only an hour or two of programming and testing, it was able to save me three or four minutes of work! Still, it was an interesting exercise in using the Path class, regular expressions, and the os module.

Download the example to experiment with it and to see additional details.

© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.