fbpx

Python automation Script to Rename Files

Renaming files can be a boring task especially if there is a long list of files that need a new name. In this article, you will learn how t create a python automation script to rename your files in one click.

Why IT Automation using Python

Python is one of the powerful programming languages in IT. It can be used for many fields including web development, desktop, and mobile application, networking, data science, AI, and Automation. What makes python a good candidate for IT automation is that there is a lot of libraries and package designed specifically for automation solutions.

Python Automation Script to Rename Files.

This is a script for renaming files in a directory using Python. Make sure your Python is installed and working properly in your machine.
■ First import the OS module. the OS module is a built-in module in python that allows you to interact with the operating system using Python.
import os

■ Now Change your working directory to where the files are located. ” I recommend first to test the script with casual files so that you can avoid any unwanted errors. type the name of the folder I inside os.chdir() method. but first, place the script in where the target folder/files are located.

os.chdir('./files test folder/')
print working directory Just to make sure you are in the right place.
print("\n\nCurrent Working Directory: " + "\n--------------------" + "\n" + os.getcwd() + "\n\n")
See all files in a list form.
print(os.listdir())

■ Now declare a variable, that can be used as a counter for each file. If a file is already named then add one. After comes a for loop to iterate over all files in the folder. The enumerate function returns 2 values: the position of each item in the folder and the item itself, which we declared as a file. Then if the statement to check the file is already contacting the name we defined at the bottom.

x = 0
for count, file in enumerate(os.listdir()):
    # Check if the file already named, if yes add 1to x counter.
    if "_PLE" in file:
     x += 1

■ If the file is not already named then it will be renamed. we use the os.path.splitext() function to separate file from it’s extension. so that we can modify the name as we want. variable file_name holds the file name, and file_ extension holds the file extension. Then you can do what you want with the file name, like appending a name to it or using a counter to give each file its number.

After creating the new name form, simply change the file name by concatenating the file and extension. Then use the os.rename(file, new_name) to rename the file.

    else:
# This 2 lines are optional you can comment them.
        print(count, file)
        print(os.path.splitext(file))
# Split file name from extension and store them in a variables.
        file_name, file_extention = os.path.splitext(file)
        file_name = file_name + "_PLE"
# Rename file name by Contating name and extention.
        new_name = f'{file_name}{file_extention}'
        os.rename(file, new_name)

■ The last step is to print the number of files that already named if they exist.

# print number of files that are named before.
if x > 0:
    print("\n"+ str(x) + " Files lready named.")

Run the Script and Automate Renaming Files with Python

 1. Place the script in the directory where target files exist.
 2. Place files in a directory so that the script can access them.
 3. Go to line 17 and change os.chdir() paramter to files directory name.
 4. Go to line 34 and change the position and value of text depending on your need.
 5. Execute the script from the command using the command:
C:\Users\Admin>python rename_files.py
Scroll to Top