What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Rename File in Python? (with OS module)

  • Feb 11, 2023
  • 5 Minute Read
How to Rename File in Python? (with OS module)

Python is a powerful and dynamic language that allows us to perform a plethora of actions and operations. A much-needed set of operations in python is file handling. It is important as files are the basic building block of any system.

In this article, we will learn a fundamental action in file handling, how to rename files with code. Before we dive into let us have a brief about what are files and why we need to rename them.

Why do we need to rename files?

A file is a grouping of data or information stored on a computer or electronic device. They are typically saved with a specific file extension, such as .txt for text files, .jpg for images, and .mp3 for audio files, to assist the operating system and other software in determining the type of file and how to open or process it.

In Python, a file is an object that contains a collection of data, stored on a disk with a specific name and file path. We can read, write, and append data to the file.

We may need to rename files for a variety of reasons:

  1. To be more descriptive or meaningful, or to follow a naming convention
  2. Organize them by grouping similar files together.
  3. Avoid having duplicate file names.
  4. Make it difficult for others to identify its contents for security reasons.
  5. Efficient management of large amounts of data by making them easier to find.

So, in short, renaming files can help us keep organized, enhance productivity, and make it easier to discover and use the files we need, whether we're working on a personal or business project.

Files can be renamed in python using the os module. Let us discuss in detail the os module before moving forward.

What is the OS module?

Python's OS module supports a variety of file system interaction operations, such as creating, renaming, and removing files and directories.

For example, the os.listdir() method, which returns a list of files and directories in a specified directory, is one of the most widely used functions in the OS module.  This module also includes the os.path submodule, which offers a number of file path-related functions.  The os.chmod() function can be used to change a file's permissions.

Python's OS module is a strong tool for interfacing with the operating system and file system. It has a lot of features for working with files and directories, file paths, environment variables, and other low-level features. Developers frequently utilize this module to automate activities, manage files and directories, and execute various other operations on the operating system.

How to Rename a file in Python?

Renaming files in Python is done using os.rename() function in the OS module. It takes two arguments: the current name of the file or directory and the new name. 

The following is the fundamental syntax:

os.rename(current_file_name, new_file_name)


It should be noted that the os.rename() function only works with files and folders in the same directory. If you want to rename a file or directory in another directory, include the entire path to the file or directory in both the current and new file name parameters.

Assume you want to rename a file named "old file.txt" to "new file.txt". The following code can be used:

import os

os.rename("old_file.txt", "new_file.txt")


You can use a loop to iterate through a list of file names and execute the os.rename() function for each file if you want to rename numerous files at once.

import os

old_file_names = ["old_file1.txt", "old_file2.txt", "old_file3.txt"]

new_file_names = ["new_file1.txt", "new_file2.txt", "new_file3.txt"]

for i in range(len(old_file_names)):

os.rename(old_file_names[i], new_file_names[i])


It is vital to remember that if the file you are attempting to rename does not exist, the os.rename() function will throw a FileNotFoundError. To handle this issue, a try-except block can be used to capture the problem and print a message to the user.

import os


os.rename(old_file_name, new_file_name)

except FileNotFoundError:

print(f"{old_file_name} does not exist.")


Also, you can check out how to delete a file in python.


When renaming files, it is critical to check whether the new file name already exists or not, as it will replace any existing file with the same name without warning. The Python OS module makes it simple and efficient to rename files. 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Kusum Jain
I am an avid coder on CodeChef, I partake in hackathons regularly. I am an independent and self-motivated student with a love for coding and experiencing new things.