| by Arround The Web | No comments

How to Rename File in Python

The os module in Python provides a way to interact with the operating system. It offers a range of functions for performing tasks such as reading, writing, creating, and deleting files. One of the functions in the os module is called os.rename(), which allows for renaming files and directories in Python.

This article covers the usage of the os.rename() function in Python, including several examples demonstrating how to rename files.

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

In Python, the os.rename() function is utilized to change the names of both files and directories. The syntax for this function is as follows:

os.rename(original_file, modified_file)

 
In the given syntax, the first parameter corresponds to the current name of the file or directory, while the second parameter represents the desired new name.

Now, let’s explore a few examples to know the process of renaming files in Python.

Renaming a File in a Directory

Now we will rename a file that is placed inside the current working directory of the Python file. As both the Python file and the text file are in the same directory, we don’t have to specify the complete path to access the file. We just access the file using its name and extension of the file.


Open the Python compiler and run the code:

import os
path1 = r"linuxhint.txt"
path2 = r"linuxhintTutorial.txt"
os.rename(path1, path2)

 
Here this code will modify the name of the file which is placed inside the current working directory of the Python file. The code uses the os.rename() function, original name and modified file name are passed as its argument.

Here we can see the file is renamed from linuxhint.txt to linuxhintTutorial.txt.

Renaming a File at a Specific Path

Now we will specify the path of the text file because it is not placed inside the current working directory of the Python file. Here we will rename the file using the os.rename() function.


The below Python code will rename a text file testfile.txt to linuxhint.txt file.

import os
path1 = r"C:\Users\Kashif Javed\Documents\testfile.txt"
path2 = r"C:\Users\Kashif Javed\Documents\linuxhint.txt"
os.rename(path1, path2)

 
The above code uses the os.rename() function, and we have passed both file paths as its argument.

In output we can see the file name has changed.

Renaming Multiple File Names in a Directory

We can also rename the multiple files which are placed inside the same directory by defining their complete path. Next, we will use a for loop to rename the files one by one.

The below image shows three sample files which will be now renamed.


Now in the code below we will specify the path of these files and rename them.

import os
 
directory = r'C:\Users\Kashif Javed\Desktop\python'
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 provided code, the function os.listdir() is used to obtain a list of all files within a directory. Next, a for loop is utilized to iterate through each file. An if statement examines whether a file possesses a .txt extension. If this condition is met, the os.rename() function will assign a new name to the file.

Here we can see that all files have been renamed successfully.

Changing the Extension of a File

Using the os.rename() function, we can also rename the file extension. Now we will take the same above file and change the file’s extension from .txt to .csv file.

Following are the three sample text files.


Now the below code will change the files extension:

import os
path_direct = r'C:\Users\Kashif Javed\Desktop\python'
 
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 given code, the function os.listdir() can read all files in a folder. Then, using a loop, each file in the list is checked. If a file has a .txt extension, it is replaced with a .csv extension using the file.replace() function.

We can see that all three file extensions have been changed successfully from .txt to .csv file.

Conclusion

This article covers methods of renaming files using the os.rename() function. It covers various scenarios, including renaming files in the current working directory, renaming files at specific paths, renaming multiple files in a directory, and even changing file extensions. The examples provided explain different scenarios of renaming file names in Python using the os.rename(). Read more about renaming files in Python in this article.

Share Button

Source: linuxhint.com

Leave a Reply