Python os.rename() Method
The os.rename()
method is used to rename a file or directory. The os.rename() is a built-in function of the operating system module and comes default with Python.
Note: The os.rename()
method will raise a FileNotFoundError
if the src file or directory does not exist, or a PermissionError
if you do not have permission to rename the file or directory.
Syntax
The syntax of os.rename()
method is:
os.rename(src, dst, src_dir_fd=None, dst_dir_fd=None)
Parameters
The os.rename()
method can take four parameters.
- src: The current file name or directory you want to rename. It could be a path-like object representing the file system path.
- dst: The new name you want to give to the file or directory. It could be a path-like object representing the file system path.
- src_dir_fd (optional): A file descriptor referring to the source directory.
- dst_dir_fd (optional): A file descriptor referring to the destination directory.
Return Value
The os.rename()
method does not return anything.
Example 1 - How to use the os.rename()
method to rename a file
# import os module
import os
# Rename a file
os.rename("old_file.txt", "new_file.txt")
# Rename a file with absolute path
os.rename("c:\\Python\\doc.txt", "c:\\Python\\doc_updated.txt")
Example 2 - How to use the os.rename()
method to rename a directory
# import os module
import os
# Rename a directory
os.rename("old_directory", "new_directory")
# Rename a directory with an absolute path
os.rename("c:\\Python", "c:\\Python3")
Example 3 - How to Handle FileNotFoundError
and PermissionError
while using os.rename()
method
The os.rename()
method will raise a FileNotFoundError
if the src file or directory does not exist or a PermissionError
if you do not have permission to rename the file or directory.
To handle errors that might occur while renaming a file or directory, you can use a try-except
block as shown in below example.
import os
# Rename a file and handle errors
try:
os.rename("old_file1.txt", "new_file.txt")
except FileNotFoundError:
print("The specified file does not exist.")
except PermissionError:
print("You do not have permission to rename the file.")
Output
The specified file does not exist.
This will catch any FileNotFoundError
or PermissionError
that might occur while renaming the file.
You can use the os.path.exists()
method to check if a file or directory exists before attempting to rename it, like this:
# import os module
import os
# Check if the file exists before renaming it
if os.path.exists("old_file.txt"):
os.rename("old_file.txt", "new_file.txt")
else:
print("The file 'old_file.txt' does not exist.")
Output
The file 'old_file.txt' does not exist.
This will ensure that the os.rename()
method will get executed only if the file exists.
Example 4 - Renaming a file with a different file extension
To change a file's extension, you can specify the new file extension in the dst argument, as shown in the example below.
# import os module
import os
# Rename a file with a different file extension
os.rename("old_file.txt", "old_file.csv")
Example 5 - Renaming multiple files at once
To rename multiple files at once, you can use a loop and the os.rename()
method, as shown in the below example. In addition, you can add your own logic using a regular expression to find and rename the files.
# import os module
import os
# Rename multiple files
for i in range(1, 4):
old_file = f"old_file_{i}.txt"
new_file = f"new_file_{i}.txt"
os.rename(old_file, new_file)
Conclusion
The os.rename()
method is a built-in function of the operating system module, mainly used to rename a file or a directory.