os.rename() method in Python
In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature that depends on the operating system. This module's main feature is interacting with the file and path directory. In this article, we will learn about Python's os.rename features.
Rename file in Python
In order to rename a file, the first thing we need is the path of the file. The path is the location of the file on the disk in a computer system. To be more particular, an Absolute path contains the complete directory list required to locate the file, and a Relative path contains the current directory and then the file name.
The os.rename() method
In Python, the os module provides a method to rename the file and directory name through the python program.
Syntax for os.rename() method
We can implement the os.rename() method by the use of the following syntax. The syntax is as follows:
os.rename(source, destination, *, src_dir_fd = None, dst_dir_fd = None)
Parameter for os.rename() method
There is some parameter that is used in the os.rename() method. These parameters are as follows:
- Source: It is a path that shows the path of the file system. We have to rename that path through the python program.
- Destination: It is also a path-like object which represents the path of the file system.
- src_dir_fd (optional): It is a type of file descriptor that refers to the directory.
- dst_dir_fd (optional): It is also a type of file descriptor that refers to the directory.
- Return Type: This method does not have any return value.
Example 1:
# Python program to explain os.rename() method
# importing os module
import os
# Source file path
source = 'javatpoint/demo.txt'
# destination file path
dest = 'javatpoint/newdemo.txt'
# Now rename the source path
# to destination path
# using os.rename() method
os.rename(source, dest)
print("Source path renamed to destination path successfully.")
Output:
Source path renamed to destination path successfully.
Example 2:
# Python program to explain os.rename() method
# importing os module
import os
# Source file path
source = './javatpoint/demo.txt'
# destination file path
dest = './javatpoint/dir'
# try renaming the source path
# to destination path
# using os.rename() method
try :
os.rename(source, dest)
print("Source path renamed to destination path successfully.")
# If Source is a file
# but destination is a directory
except IsADirectoryError:
print("Source is a file but destination is a directory.")
# If Source is a directory
# but destination is a file
except NotADirectoryError:
print("Source is a directory but destination is a file.")
# For permission-related errors
except PermissionError:
print("Operation not permitted.")
# For other errors
except OSError as error:
print(error)
Output:
Source path renamed to destination path successfully.
Example 3:
import os, sys
# listing directories
print "The dir is: %s"%os.listdir(os.getcwd())
# renaming directory ''sonudir"
os.rename("sonudir","sonudirectory")
print "Successfully renamed."
# listing directories after renaming "sonudir."
print "the dir is: %s" %os.listdir(os.getcwd())\
Output:

Renaming only the Extension of the file in Python
Sometimes, we just want to rename the extinction name through the help of the os.rename() method. This can be done by performing the split text operation in the program. Then only we can rename the extension name. This os method returns both the root and extension name separately. Once we get the root name of the file path, then we can add the new extension name to the file path with the help of the os.rename() method.
Example 4:
import os
# Selecting the list
print('Before rename:')
file = file.txt
print(file)
# Renaming the file
for file_name in file:
# construct full file path
old_file_name = os.path.join(folder, file_name)
# Change the Extension from txt to pdf
new_file_name = old_file_name.replace('.txt', '.pdf')
os.rename(old_file_name, new_file_name)
print('After rename:')
print(file)
Output:

Renaming a file in Python is as easy as naming a file. The Os module in Python renames a file name and other functions.