Python – os.replace() method Last Updated : 21 Oct, 2021 Comments Improve Suggest changes Like Article Like Report Prerequisite: OS module in Python. os.replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission. This method may fail if the source and destination are on different filesystems. Syntax: os.replace(source, destination, *, src_dir_fd=None, dst_dir_fd=None2) Parameter: source: Name of file or directory which we want to rename. destination: Name which we want to give in destination. src_dir_id : This parameter stores the source directory’s or file, file descriptor referring to a directory. dst_dir_fd: It is a file descriptor referring to a directory, and the path to operate. It should be relative, path will then be relative to that directory. If the path is absolute, dir_fd is ignored. Return Type: This method does not return any value. Code #1: Use of os.replace() method to rename a file. Python3 # Python program to explain os.replace() method # importing os module import os # file name file = "f.txt" # File location which to rename location = "d.txt" # Path path = os.replace(file, location) # renamed the file f.txt to d.txt print("File %s is renamed successfully" % file) Output: File f.txt is renamed successfully Code #2: Handling possible errors. (If necessary permissions are given then output will be as shown in below) Python # Python program to explain os.replace() method # importing os module import os # Source file path source = './file.txt' # destination file path dest = './da' # try renaming the source path # to destination path # using os.rename() method try: os.replace(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: Output: Source is a file but destination is a directory. Comment More infoCampus Training Program Next Article Python | os.sync() method coder36 Follow Improve Article Tags : Python python-os-module Practice Tags : python Similar Reads cv2.imread() method - Python OpenCV OpenCV-Python is a Python library used to solve computer vision tasks. cv2.imread() method loads an image from the specified file. If the image cannot be read because of m... 12 min read Python | os.rename() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | os.read() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | os.renames() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | os.readv() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | os.remove() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | os.write() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 1 min read Python | os.truncate() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | os.sendfile() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way o... 2 min read Python | sympy.replace() method With the help of sympy.replace() method, we can replace the functions in the mathematical expression without editing the whole expression by using sympy.replace() method.... 1 min read Like