Python os.path.isabs() Method

Python os.path.isabs() Method

  1. Syntax of os.path.isabs() Method
  2. Example Code: Use the os.path.isabs() Method to Check Specified Path
  3. Example Code: Use the os.path.isabs() Method With if Condition
  4. Example Code: Use the os.path.isabs() Method to Check Valid Directory
  5. Example Code: Difference Between isabs() and is_absolute() Methods

The isabs() method is used to check whether the provided path is an absolute path or not. The absolute path is the one that shows full details of the location of the file or directory.

It shows starting point, i.e., the root(parent) element to the ending subdirectories.

Syntax of os.path.isabs() Method

os.path.isabs(path)

Parameters

path A specified path showing any file system directory.

Return

This method returns a Boolean value, either True if the specified path is an absolute path or False if the path is not absolute.

Example Code: Use the os.path.isabs() Method to Check Specified Path

# importing os.path module
import os.path

# Full path starting from drive to file name
check_path = "C:/Users/lenovo/Downloads/main.py"
#passing the path to the method
check_abs = os.path.isabs(check_path)
print(check_abs)

# Path with drive name - root to subdirectory
check_path = "C:/Users/lenovo/Downloads/"
#passing the path to the method
check_abs = os.path.isabs(check_path)
print(check_abs)

# Not a full path, includes slash symbol for checking
check_path = "/Users/lenovo/Downloads/"
check_abs = os.path.isabs(check_path)
print(check_abs)

# Again, not a full path, includes no slash symbol or drive name
check_path = "Users/lenovo/Downloads/"
check_abs = os.path.isabs(check_path)
print(check_abs)

Output:

True
True
True
False

The method returns whether the specified path is an absolute path or not. An absolute path can start with the drive name and follow the subdirectories path, or it can start with a slash symbol (/).

Example Code: Use the os.path.isabs() Method With if Condition

import os.path

# list of path directories
check_path = ["C:/Users/lenovo/Downloads/", "/xyz", "./zzz"]
for i in range(len(check_path)):
    #check if each path is absolute or not
    if(os.path.isabs(check_path[i])):
        print("This directory is an absolute path.")
    else:
        print("Not an absolute path.")

Output:

This directory is an absolute path.
This directory is an absolute path.
Not an absolute path.

We have a list of paths, and each path is checked with the os.path.isabs() method and access different conditions accordingly.

Example Code: Use the os.path.isabs() Method to Check Valid Directory

import os
#sent a random value as a parameter to this method with a slash symbol
print(os.path.isabs("//\\\\xmls\\hello.txt"))
#no slash symbol
print(os.path.isabs("\\\\xmls\\"))
#value with the drive name
print(os.path.isabs("C://\\\\xmls"))
#value with no drive or symbol
print(os.path.isabs("a/b"))

Output:

True
False
True
False

The method returns True for the said parameter. The reason is that the os.path.isabs() method considers the slash (/) symbol at the start as an absolute path.

The method does not check if it is a valid path or not. So, the returned value may be an absolute but not a valid directory.

Example Code: Difference Between isabs() and is_absolute() Methods

# Import pathlib module
import pathlib
#check whether the is_absolute() method on current file path
path = pathlib.Path(__file__)
print(path.is_absolute())
#check a particular location
path = pathlib.PurePath('C:/Users/Downloads/main.py')
print(path.is_absolute())

Output:

True
True

We use the os.path module to interact with different python methods, and the PurePath module also provides different methods to work with. However, it is preferable to work with the Purepath method as it has more support with Python 3.1+ versions.

The PurePath performs operations on path strings, and the path accesses the original file system.

Author: Junaid Khan
Junaid Khan avatar Junaid Khan avatar

Hi, I'm Junaid. I am a freelance software developer and a content writer. For the last 3 years, I have been working and coding with Python. Additionally, I have a huge interest in developing native and hybrid mobile applications.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method