Rename multiple files in a directory in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Renaming many files in Python is straightforward, but safe bulk renaming needs a little more discipline than a single os.rename call. The real concerns are predictable ordering, name collisions, and being clear about which files should change. A good batch rename script previews the result, uses explicit path handling, and avoids accidentally overwriting existing files.
A Simple pathlib Pattern
pathlib is a clean modern choice for filesystem work.
This prefixes every .txt file in reports with archived_.
Rename in a Stable Order
If the new names depend on numbering, sort the files first. Filesystem iteration order is not something you should rely on.
This produces names like image_001.jpg, image_002.jpg, and so on.
Preview Before Renaming
A dry run is often the difference between a useful utility and a destructive one.
Run the preview first. Once the mapping looks correct, replace the print with rename.
Avoid Name Collisions
A bulk rename can fail or overwrite files if the destination names already exist.
This kind of explicit check is important when renaming into a naming scheme that might already be present.
Pattern-Based Renaming
Sometimes you only need a string replacement in each filename.
This is simple and effective as long as the replacement rule is unambiguous.
Two-Phase Renames Help with Cycles
If one rename would collide with another, a two-phase strategy is safer. First rename everything to temporary names, then rename to the final names.
This is useful when the final names overlap with names that already exist in the same directory during the rename operation.
os.rename Versus Path.rename
os.rename and Path.rename do the same basic job. pathlib usually reads better because the path manipulation methods are attached to the path object itself.
If you are writing new code, pathlib is often the cleaner default.
Common Pitfalls
- Renaming files without sorting first when the new names depend on sequence numbers.
- Running a bulk rename without a dry run and discovering mistakes only after the files changed.
- Overwriting or colliding with existing filenames because the script never checked the target paths.
- Applying a broad rename rule to directories when only files should have been changed.
- Using ambiguous string replacements that accidentally rename the wrong parts of filenames.
Summary
- Use
pathlibfor clear and maintainable bulk rename scripts. - Sort files before assigning numbered names.
- Preview the rename mapping before applying it.
- Check for collisions so you do not overwrite files accidentally.
- Keep the rename rule narrow and explicit so the batch operation stays predictable.
Related reading
- Rename Pandas DataFrame Index
- Rename specific columns in pandas
- Renaming column names in Pandas
- Repeating triangle pattern in Python
- Replace a character at a specific index in a string?
- Replace all elements of NumPy array that are greater than some value
- Replace list of list with condensed list of list while maintaining order
- Replacements for switch statement in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.