Python os.path.isabs() Method

Python os.path.isabs() method is used to check whether a given path is an absolute path.

An absolute path is a path that specifies a location on the filesystem independently of the current directory.

The concept of absolute paths is different in Unix-like systems and Windows.

For example, in Unix/Linux, an absolute path starts with / (the root directory), whereas in Windows, it starts with a drive letter followed by : and \ (like C:\).

Syntax

os.path.isabs(path)

Parameters

Name Description
path (string) It is the path that you want to check.

Return value

It returns True if the path is an absolute path.

It returns False if the path is a relative path.

Example 1: Checking an Absolute Path (Unix/Linux)

Visual Representation of Python os.path.isabs() Method

import os

path = '/home/user/file.txt'

print(os.path.isabs(path))

Output

True

Example 2: Checking a Relative Path

Checking a Relative Path

import os

path = './file.txt'

print(os.path.isabs(path))

Output

False

Example 3: Checking an Absolute Path (Windows)

Checking an Absolute Path For Windows

import os

path = 'C:\\Users\\user\\file.txt'

print(os.path.isabs(path)) 

Output

False

That’s all!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.