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 at 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 WebPage or 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: Organize a Messy Folder: using Junk File Organizer in Python. I hope it will help you a lot.

Thanks for reading!💙

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post