How to Rename Files in Python?

To interact with the operating system in Python, the “os” module is used. This module offers various functions for reading, writing, creating, or deleting files in Python. The “os” module delivers a function named “os.rename ()” that is used to rename the files and directories in Python. 

This post will address the os.rename() function alongside various examples to rename files in Python. 

How to Use os.rename() to Rename Files in Python?

The “os.rename()” function is used to rename the “files” and “directories” in Python. The syntax of the “os.rename()” function is shown below:

os.rename(original_file, modified_file)

In the above syntax, the first parameter represents the current name of the source file, and the second parameter represents the new name of the file.

Let’s understand how we rename files in Python via the following examples.

Example 1: Rename the File in the Directory

In the below snippet, the source file is placed in Python’s current working directory.

Note: If the file is placed in the current working directory of Python, then we don’t need to specify the file’s complete path. We just use the file name with the extension.


import os
path1 = r"sample.txt"
path2 = r"itslinuxfoss.txt"
os.rename(path1, path2)

In the above code, the original name and modified file name are placed as the first and second argument in the “os.rename()” function.


The file has been renamed from “sample.txt” to “itslinuxfoss.txt”.

Example 2: Rename the File Placed at Specific Path

In the example given below, the “os.rename()” function renames the selected file at the specified path.

The below snippet shows the path of the “sample.txt” file:


import os
path1 = r"C:\Users\p\Documents\ILF\sample.txt"
path2 = r"C:\Users\p\Documents\ILF\itslinuxfoss.txt"
os.rename(path1, path2)

In the above code, the “os. rename()” function accepts the source file path as a first argument and the new name along with complete destination path as a second argument.


The above output shows that the file name has been successfully renamed.

Example 3: Rename the Multiple File Name in the Directory

To rename the multiple files placed in the same directory, the “os.rename()” function is used along with the for loop. 

The snippet below shows the targeted files’ file name and path:


import os

directory = 'C:/Users/p/Documents/ILF'
extension = '.txt'

for filename in os.listdir(directory):
    if filename.endswith(extension):
        old_file = os.path.join(directory, filename)
        new_file = os.path.join(directory, filename.replace(extension, '_new.txt'))
        os.rename(old_file, new_file)

In the above code, the “os.listdir()” function lists all the directory files, and a for loop is used to iterate over the files. The “if” statement checks if the file has a “.txt” extension; if yes, then the “os. rename()” function renames the file with the new name.


The multiple files have been successfully renamed.

Example 4: Rename the Extension of the File

The “os.rename()” function is used in the below code to rename the file’s extension.

The below snippet shows the file name and path:


import os
path_direct = 'C:/Users/p/Documents/ILF'

for file in os.listdir(path_direct):
    if file.endswith('.txt'):
        new_filename = file.replace('.txt', '.csv')
        os.rename(os.path.join(path_direct, file), os.path.join(path_direct, new_filename))

In the above code, “os.listdir()” function lists all the files stored in a specific directory. The for loop iterates over the list of files and replaces the “.txt” extension with the “.csv” extension using the “file.replace ()” function.


The above output verified that the “.txt” file had been successfully renamed to the “.csv” file extension.

Example 5: Renaming files with a timestamp

We can also add the specific timestamp while renaming the file in Python. The below code example uses the “os.rename ()” function and “datetime module” to rename the files with timestamp:

The below snippet shows the source file:


import os
import datetime
timestamp ='%d-%b-%Y')
os.rename(r"sample.txt", r"sample" + timestamp + ".txt")

In the above code, the “os” and “datetime” module is imported in the program. The “” function returns the current data. The “os.rename()” function renames the old file name with the new name and a timestamp.


The above snippet shows the file with a timestamp.


The “os.rename()” function is used in Python to rename a specific file in a directory, multiple files, the extension of the file, etc. The ‘os.rename()‘ function is used along with the ”os.listdir()” function and for loop to rename the multiple files in the specified directory. This post presented a detailed guide on how to rename files in Python.