Python os.path.relpath() Method

Musfirah Waseem Jan 30, 2023
  1. Syntax of Python os.path.relpath() Method
  2. Example 1: Use the os.path.relpath() Method in Python
  3. Example 2: Not Specifying the start Parameter in the os.path.relpath() Method
  4. Example 3: Try Various Directories for the start Parameter in the os.path.relpath() Method
  5. Example 4: Enter the Same Parameters in the os.path.relpath() Method
Python os.path.relpath() Method

Python os.path.relpath() method is an efficient way to return a relative file path to a specified directory either from the stated directory or from the current working directory. In this method, the existence of a given path isn’t checked.

Syntax of Python os.path.relpath() Method

os.path.relpath(path)
os.path.relpath(path, start)

Parameters

path It is an address object of a file system path or a symlink. The object can either be an str or bytes.
start (Optional) It is an address object of a file system path or a symlink. The relative path for a given path will be computed concerning the directory indicated. By default, os.curdir is used, a constant string used by the OS to refer to the current directory.

Return

The return type of this method is a string value representing the relative file path to the stated parameter from the start directory.

Example 1: Use the os.path.relpath() Method in Python

import os

path_address = "/home/Admin/Documents/File.txt"

start = "/home/Admin"

relpath = os.path.relpath(path_address, start)

print("The relative path is: ", relpath)

Output:

The relative path is:  Documents/File.txt

The ValueError exception on Windows OS is raised when the path and the start parameters are on different drives.

Example 2: Not Specifying the start Parameter in the os.path.relpath() Method

import os

path_address = "/home/Admin/Documents/File.txt"

relpath = os.path.relpath(path_address)

print("The relative path is: ", relpath)

Output:

The relative path is:  ../../Admin/Documents/File.txt

The start parameter is not provided in the above code, so the current working directory is used alternatively.

Example 3: Try Various Directories for the start Parameter in the os.path.relpath() Method

import os

path_address = "/home/Admin/Documents/File.txt"

start = "Desktop/home"

relpath = os.path.relpath(path_address, start)

print("The relative path is: ", relpath)

path_address = "/home/Admin/Documents/File.txt"

start = "/home/Admin/Desktop/File.txt"

relpath = os.path.relpath(path_address, start)

print("The relative path is: ", relpath)

Output:

The relative path is:  ../../../../Admin/Documents/File.txt
The relative path is:  ../../Documents/File.txt

In the above code, the directories of the path and start parameters are different, so the relative path is chosen based on any similar base directory.

Example 4: Enter the Same Parameters in the os.path.relpath() Method

import os

relpath = os.path.relpath("C:\\Windows", "C:\\Windows")

print("The relative path is: ", relpath)

Output:

The relative path is:  .

Note that if the path provided is relative, the function returns a . full stop punctuation.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python OS