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.
But when I go to upload, see what happens.
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
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!💙