Python, a robust programming language, allows users to perform numerous operations such as file handling, web scraping, etc. Files are the fundamental units of a system. A file is an instance that consists of a collection of data with different file extensions such as .jpg, .txt, etc
Why Do We Need to Rename Files in Python?
We need to rename files in Python for the following purposes:
- To be more significant or descriptive.
- Refrain from using duplicate filenames.
- Successful handling of massive amounts of data
This article will demonstrate how to rename a file using Python.
How Can We Use Python To Rename a File?
Python provides us with two methods to rename a file:
- Using os.rename() Method
- Using Shutil Module
Let’s discuss them one by one.
Method 1: Use os.rename() To Rename a File
A file in Python can be renamed using the os.rename() function. This method takes the old file name or directory and the new file name as parameters.
The syntax is shown below:
os.rename(current_file_name, new_file_name)
Note: os.rename() method executes only if the files and folders are in the current directory. You must specify the complete path if they are in a separate directory.
Let’s see an example to demonstrate this practically.
Example 1: Rename a Single File Using os.rename() Method
The following code utilizes the os.rename() function to rename the “requirements.txt” file to “new_file.txt”:
Import os
os.rename('requirements.txt','new_file.txt')
Before executing the above code:
After executing the above code:
It can be seen that the name of the file has been changed.
Example 2: Rename Multiple Files Using os.rename() Method
Let’s see an example when multiple files are renamed simultaneously. To do that, use a for loop.
import os
current_file_names = ["file1.txt", "file2.txt", "file3.txt, image.jpg"]
new_file_names = ["new_file1.txt", "new_file2.txt", "new_file3.txt”, “wallpaper.jpg"]
for i in range(len(current_file_names)):
os.rename(current_file_names[i], new_file_names[i])
In the above code:
- The os module is imported.
- The three files that we want to rename are provided in a list called curent_file_names.
- The new names are provided in another list named new_file_names.
- The length of the list is iterated through using a for loop.
- Os.rename method is used to rename the current_file_names to new_file_names.
Before executing the above code:
After executing the above code:
It can be seen that the names have been changed successfully.
Method 2: Use the Shutil Module To Rename a File
The shutil module provides a move() function that helps to rename the file names.
The syntax is shown below:
shutil.move(old_file_name, new_file_name)
Example: Using shutil() module to Rename A File in Python
Here’s an example:
import shutil
a = 'new_file1.txt'
b = 'main_python_file.txt'
shutil.move(a,b)
In the above code:
- We first import the shutil module
- The variable a consists of the old file name in “”.
- The variable b contains the new file name in “”.
- move() method is used to rename the file from “new_file1.txt” to variable “main_python_file.txt”.
Before executing the above code:
After executing the above code:
It can be seen that the file name has been changed successfully.
Conclusion
Files are the fundamental units of a system. To change a file name in Python, we can use two methods. The first method is by using the os.rename() which renames a file by taking both the old file and new file names as arguments. In the next method, the shutil module is used. This module has a function move() which changes the file name by taking the old file name and the new file name.