In this post, we’ll walk through how to rename multiple files at once in Python using the os
and argparse
modules. The utility will allow flexible renaming based on patterns, prefixes, suffixes, and numbering.
Create a Bulk Rename Utility in Python
Getting Started
Renaming multiple files at once is a frequent task, particularly when managing large sets of photos, documents, or data files. Although there are many graphical tools available, building your own Bulk Rename Utility in Python(bulk rename tool) is an excellent way to automate the process and enhance your scripting abilities.
Bulk File Rename Code:
This tool uses only Python’s standard library, so no external packages are needed. Tested on Python 3.8
import os
import argparse
def bulk_rename(
directory,
prefix="",
suffix="",
replace_from="",
replace_to="",
start_number=1,
number=False,
dry_run=False,
):
if not os.path.isdir(directory):
print(f"Error: {directory} is not a valid directory.")
return
files = sorted([f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))])
if not files:
print("No files found in the directory.")
return
for index, filename in enumerate(files, start=start_number):
name, ext = os.path.splitext(filename)
new_name = name
# Apply replacement
if replace_from:
new_name = new_name.replace(replace_from, replace_to)
# Add prefix and suffix
new_name = f"{prefix}{new_name}{suffix}"
# Add numbering
if number:
new_name = f"{new_name}_{index}"
new_filename = new_name + ext
src = os.path.join(directory, filename)
dst = os.path.join(directory, new_filename)
if dry_run:
print(f"[Dry Run] {filename} -> {new_filename}")
else:
os.rename(src, dst)
print(f"Renamed: {filename} -> {new_filename}")
print("Done.")
def main():
parser = argparse.ArgumentParser(description="Bulk Rename Utility in Python")
parser.add_argument("directory", help="Path to the directory containing files to rename")
parser.add_argument("--prefix", default="", help="Prefix to add to file names")
parser.add_argument("--suffix", default="", help="Suffix to add to file names")
parser.add_argument("--replace-from", default="", help="Substring to replace in file names")
parser.add_argument("--replace-to", default="", help="Replacement substring")
parser.add_argument("--start-number", type=int, default=1, help="Starting number for numbering")
parser.add_argument("--number", action="store_true", help="Append incremental number to file names")
parser.add_argument("--dry-run", action="store_true", help="Preview changes without renaming files")
args = parser.parse_args()
bulk_rename(
args.directory,
prefix=args.prefix,
suffix=args.suffix,
replace_from=args.replace_from,
replace_to=args.replace_to,
start_number=args.start_number,
number=args.number,
dry_run=args.dry_run,
)
if __name__ == "__main__":
main()
How it Works
This Python bulk rename tool includes the following features:- Rename all files in a directory.
- Add a prefix or suffix to file names.
- Replace a substring in file names.
- Add incremental numbering.
- Optionally preview changes before applying them.
Summary
Creating a bulk file renamer in Python is not only practical but also a great way to learn how to manipulate the filesystem, use argument parsing, and write user-friendly command-line tools.
Thanks