Rename Multiple Files at Once using a Python Program

Rename multiple files in python. It can perform a time consuming task in a few seconds

Introduction

Today we’ll see a real-life problem at very first. Those of us who use computers have used the WhatsApp Web at least once. If so, then you might have seen how the names of the photos look like downloaded from there.

Not only for WhatsApp, but it also happens with the images downloaded from the web. For the reasons, we often face a problem finding a specific image when uploading photos on social media. For instance, I want to upload this image: “IMG_20211103_153001.jpg” on my Instagram page from a folder shown in the image below.

A folder containing several image files. These files will be reamed using a python code at once.

But when I go to upload, see what happens.

Uploading an image on Instagram from a folder that containing several images downloaded from Whatsapp.

I can’t figure out exactly which photo I wanted to upload. To solve this issue I’ve developed a python program for renaming multiple files in a folder at once. I used the python OS module in the program. So you don’t need to install any extra modules or libraries. Simply follow these simple steps below.

Steps

1. Copy the code from below and run it.

2. Enter the folder path where your images are present. (You can rename other files by changing the extension – see the Yellow line. For example, if you want to rename PDF files then, type .pdf in the place of .jpg)

3. Now enter the base name of every file. (For instance, if you select ‘image‘ as a base name then, the renamed file will look like, image_1.jpg, image_2.jpg, and so on)

🔹Visit Also: Extract Emails and Phone Numbers🔍 from a Text: using Python Regex

The Code


'''Rename multiple files in a folder or directory using Python'''
import os

def renamer():
    folder_path = input("Enter the Folder Path: ")
    file_name = input("Enter the Base Name of every files: ")

    for num, filename in enumerate(os.listdir(folder_path)):
        destination = f"{file_name}_{str(num)}.jpg"
        source = f"{folder_path}/{filename}"
        destination = f"{folder_path}/{destination}"

        # Rename all the files
        os.rename(source, destination)
    print("Done!")

if __name__ == "__main__":
    renamer()

Output

Renaming multiple image files in a folder using python at once.

Video Output

Do watch the entire video for a better understanding.

I hope this time it will not be difficult to find a specific photo.

🔹Visit Also: Get Hardware and System Information Using Python

Conclusion

A file can be renamed easily. But when you have to do the same thing for a lot of files, manually renaming them is a lot of time-consuming work. To save our time we developed a Python program for renaming multiple files in a folder at once.

Try to make this code more advanced. Read this article: Create a Junk File Organizer in Python using Tkinter. Create a Junk File Organizer in Python using Tkinter. I hope it will help you a lot.

Thanks for reading!💙

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147