Python | os.path.relpath() method Last Updated : 18 Jun, 2019 Comments Improve Suggest changes Like Article Like Report 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 of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation. os.path.relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory. Note: This method only computes the relative path. The existence of the given path or directory is not checked. Syntax: os.path.relpath(path, start = os.curdir) Parameter: path: A path-like object representing the file system path. start (optional): A path-like object representing the file system path. The relative path for given path will be computed with respect to the directory indicated by start. The default value of this parameter is os.curdir which is a constant string used by the operating system to refer to the current directory. A path-like object is either a string or bytes object representing a path. Return Type: This method returns a string value which represents the relative file path to given path from the start directory. Code : Use of os.path.relpath() method Python3 # Python program to explain os.path.relpath() method # importing os module import os # Path path = "/home / User / Desktop / file.txt" # Path of Start directory start = "/home / User" # Compute the relative file path # to the given path from the # the given start directory. relative_path = os.path.relpath(path, start) # Print the relative file path # to the given path from the # the given start directory. print(relative_path) # Path path = "/home / User / Desktop / file.txt" # Compute the relative file path # to the given path from the # the current directory. # if we do not specify the start # parameter it will default to # os.curdir i.e current directory relative_path = os.path.relpath(path) # Print the relative file path # to the given path from the # the given start directory. print(relative_path) # Path path = "/home / User / Desktop / file.txt" # Path of Start directory start = "GeeksForGeeks / home" # Compute the relative file path # to the given path from the # the given start directory. relative_path = os.path.relpath(path, start) # Print the relative file path # to the given path from the # the given start directory. print(relative_path) # Path path = "/home / User / Desktop / file.txt" # Path of Start directory start = "/home / User / ihritik / file.txt" # Compute the relative file path # to the given path from the # the given start directory. relative_path = os.path.relpath(path, start) # Print the relative file path # to the given path from the # the given start directory. print(relative_path) Output: Desktop/file.txt ../User/Desktop/file.txt ../../../User/Desktop/file.txt ../../Desktop/file.txt Reference: https://docs.python.org/3/library/os.path.html Comment More infoAdvertise with us Next Article Python | os.path.relpath() method I ihritik Follow Improve Article Tags : Python python-os-module Python OS-path-module Practice Tags : python Similar Reads Python | os.path.getatime() 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 of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.getmtime() 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 of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.getctime() 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 of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.size() 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 of using operating system-dependentthe functionality. os.path module is a submodule of the OS module in Python used for common path 2 min read Python | os.path.isabs() 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 of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python os.path.isfile() method The os.path.isfile() method in Python is used to check if a path refers to a regular file (not a directory or any other type of file system object). This method can be particularly useful when you need to validate file paths before performing file operations like reading, writing, or modifying files 2 min read os.path.isdir() method - Python os.path.isdir() method in Python checks whether the specified path is a directory or not. This method follows the symbolic link, which means if the specified path is a symbolic link pointing to an existing directory then the method will return True.Example: Using os.path.isdir() methodThis code demo 2 min read Python | os.path.islink() 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 of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.ismount() method os.path module is sub module of Python OS Module in Python used for common path name manipulation. os.path.ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where a different file system has been mounted. os.path.ism 2 min read Python | os.path.join() method The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). ExamplePythonimport 4 min read Like