Skip to main content

Command Palette

Search for a command to run...

Bulk File Renamer in Python – With Suffixes, Timestamps & File Type Filters

Published
4 min read
Bulk File Renamer in Python – With Suffixes, Timestamps & File Type Filters

Introduction

Renaming files one by one is tedious. Whether you're organizing photos, backing up documents, or preparing datasets, automating this task can save you time and reduce mistakes.

In this guide, we’ll build a Python-based Bulk File Renamer that:

  • Renames multiple files in a directory

  • Adds custom prefixes, suffixes, or timestamps

  • Filters by file type (e.g., only rename .jpg and .png)

  • Works with just a few lines of Python

What You'll Learn

  • How to iterate through files in a directory

  • How to rename files with numbering, suffixes, and timestamps

  • How to filter files by type

  • How to use Python’s built-in os and datetime modules

Let’s get started.

Requirements

We’ll use only built-in modules:

import os
from datetime import datetime

No third-party installation needed. This works on Windows, macOS, and Linux.

Step-by-Step Script with Explanations

Step 1: Import Necessary Modules

import os
from datetime import datetime

Explanation:

  • os is used to interact with the file system (list, rename, check file types).

  • datetime allows us to add timestamps to the renamed files.

Step 2: Set the Target Directory

folder_path = r"C:\Users\YourName\Documents\RenameMe"  # Update this to your folder path

Explanation:

  • Replace this with the path to the folder containing the files you want to rename.

  • Use r"" to prevent backslash escape errors on Windows.

Step 3: Define Renaming Parameters

prefix = "Image_"
suffix = "_backup"
use_timestamp = True
allowed_extensions = ['.jpg', '.png']  # Only rename these file types
start_number = 1

Explanation:

  • prefix: Text that comes at the start of the filename

  • suffix: Text added after the number but before the extension

  • use_timestamp: If True, adds the current date and time

  • allowed_extensions: Only files with these extensions will be renamed

  • start_number: Starting point for numbering (can be changed)

Step 4: Loop Through and Rename Files

for count, filename in enumerate(os.listdir(folder_path), start=start_number):
    file_path = os.path.join(folder_path, filename)

    if os.path.isfile(file_path):
        name, ext = os.path.splitext(filename)

        # Skip files that don't match the allowed extensions
        if ext.lower() not in allowed_extensions:
            continue

        # Format timestamp if needed
        timestamp = datetime.now().strftime("_%Y%m%d_%H%M%S") if use_timestamp else ""

        # Create new filename
        new_name = f"{prefix}{count}{suffix}{timestamp}{ext}"
        new_path = os.path.join(folder_path, new_name)

        # Rename the file
        os.rename(file_path, new_path)
        print(f"Renamed: {filename}{new_name}")

Full Script

import os
from datetime import datetime

# Set your folder path
folder_path = r"C:\Users\YourName\Documents\RenameMe"  # Change this

# Renaming configuration
prefix = "Image_"
suffix = "_backup"
use_timestamp = True
allowed_extensions = ['.jpg', '.png']  # Rename only these types
start_number = 1

# Start renaming loop
for count, filename in enumerate(os.listdir(folder_path), start=start_number):
    file_path = os.path.join(folder_path, filename)

    if os.path.isfile(file_path):
        name, ext = os.path.splitext(filename)

        # Skip unwanted file types
        if ext.lower() not in allowed_extensions:
            continue

        # Format timestamp if enabled
        timestamp = datetime.now().strftime("_%Y%m%d_%H%M%S") if use_timestamp else ""

        # Create new name with all components
        new_name = f"{prefix}{count}{suffix}{timestamp}{ext}"
        new_path = os.path.join(folder_path, new_name)

        # Rename the file
        os.rename(file_path, new_path)
        print(f"Renamed: {filename}{new_name}")

How It Works (Recap)

  • os.listdir() lists everything in the directory

  • os.path.isfile() ensures we only rename files (not subfolders)

  • os.path.splitext() separates the base name and extension

  • datetime.now().strftime() formats the timestamp as _YYYYMMDD_HHMMSS

  • Files are renamed using:
    prefix + count + suffix + timestamp + extension

Example

Let’s say you have:

Before:

photo1.jpg
screenshot2.png
notes.txt

After (with prefix "Image_", suffix "_backup", and timestamp):

Image_1_backup_20250513_174015.jpg
Image_2_backup_20250513_174015.png

Note: notes.txt is skipped because it's not in the allowed extensions list.

Best Practices

  • Test on a copy of your files first

  • Make backups before running

  • Use consistent naming rules for organization

  • Log renamed files to a .txt or .csv if you need tracking

Final Thoughts

You now have a flexible and powerful Bulk File Renamer that supports:

  • Custom prefixes and suffixes

  • Timestamping

  • File type filtering

This script is perfect for organizing media libraries, preparing datasets, renaming photoshoots, and more.

Want to expand this with a GUI or automatic logging? Just say the word — I can help you build the next version.

More from this blog

PyScript Academy

29 posts

PyScript Academy is a blog sharing practical Python scripts, tips, and mini projects—helping you learn Python by doing, one useful script at a time.