Wednesday, June 29, 2016

Python script to rename multiple files/folders

Hello,
In this post, am going to walk you through a process that could be boring, repetitive, or just pron to errors/mistakes if done manually. This process is: renaming multiple files/folders with Python script.

If you ever found yourself in a situation where you have to rename several files/folders, then this Python scripting tutorial will help you.

Let's get started!

I have a folder containing 200+ pictures from my digital camera with the picture names like this format: "IMG-20160530-WA0000.jpg" as seen below.



Obviously, each picture name contains four (4) parts separated by the hyphen sign (-).
Part 1: is a constant tag for all the images "IMG"
Part 2: is the date in which the file was created in this format YearMonthDay "20160530"
Part 3: is a unique ID that repeat counting for every new day "WA0000"
Part 4: the last part, which is the image file extension ".jpg"

The above parts makes up the name of every picture in my folder as shown above.

The task at hand is for me to rename all the files to a new format like this: Umar_Pic01, Umar_Pic02, Umar_Pic03 etc. This is so that the pictures will be easily referred to in another application.

Am going to use the python OS module. This module has functions to interact with the Operating System (OS) such as:
os.chdir() - to change working directory,
os.getcwd() - to get working directory,
os.listdir() - to list working directory, and
os.rename() - to rename a file

To get started, I will change working directory to the pictures folder, confirm working directory by printing it out and loop over the files in the working directory to be sure am working on the correct data directory. The code below will do just that;-

import os

# Lets change working directory to the pictures folder
os.chdir("C:\Users\PC\Desktop\Codes\pictures")

# confirm working directory by printing it out
print os.getcwd()

# loop over the files in the working directory and printing them out
for file in os.listdir('C:'):
 print file



Now to build on the code above, I will split the file names from the extension. That is "IMG-20160530-WA0000" separate from ".jpg" for each of the files. Lets replace/edit the 'for' loop as follow;-

import os

# Lets change working directory to the pictures folder
os.chdir("C:\Users\PC\Desktop\Codes\pictures")

# confirm working directory by printing it out
print os.getcwd()

# loop over the files in the working directory and printing them out
# for file in os.listdir('C:'):
 # print file


# loop over the files and split the names from the extension
for file in os.listdir('C:'):
 print os.path.splitext(file)



Since the split result is tuple of two elements (the file name and the extension), I will set them to two variables so we can have access to the file name for further manipulations as follow;-

import os

# Lets change working directory to the pictures folder
os.chdir("C:\Users\PC\Desktop\Codes\pictures")

# confirm working directory by printing it out
print os.getcwd()

# loop over the files in the working directory and printing them out
# for file in os.listdir('C:'):
 # print file


# loop over the files and split the names from the extension
for file in os.listdir('C:'):
 file_name, file_extension = os.path.splitext(file)
 print file_name


Now, lets create an arbitrary incremental variable "i", then use it as rename number for the files as seen below:-

import os

# Lets change working directory to the pictures folder
os.chdir("C:\Users\PC\Desktop\Codes\pictures")

# confirm working directory by printing it out
print os.getcwd()

# loop over the files in the working directory and printing them out
# for file in os.listdir('C:'):
 # print file


# loop over the files and split the names from the extension
# create an arbitrary incremental variable "i", then rename the files
i = 0
for file in os.listdir('C:'):
 file_name, file_extension = os.path.splitext(file)

 i += 1
 new_file_name = 'Umar_Pic0{}.jpg'.format(i)

 os.rename(file, new_file_name)




This same concept to rename multiple files above can be applied to renaming multiple folders.

That is it!
Thanks for reading.

3 comments:

  1. Nice article thank u umar from arjun rajanala(INDAI)

    ReplyDelete
  2. Really nice code! Just one problem: what if there are other directories in the folder with your pictures? Those end up getting renamed according to the algorithm, too...

    ReplyDelete
  3. I want to rename files in a folder. There is a excel sheet which contains the name of files in the folder to be renamed. Basically i want to rename files in a folder for which the renaming section is to be copied from excel sheet so as to rename files in a folder.

    ReplyDelete