The isabs()
function is defined in the path
sub-module of the os
module. It is used to determine whether a given pathname represents an absolute path or not.
An absolute path is one that begins from the root directory of a file system, regardless of the current working directory. In a Unix-like system, an absolute path starts with a forward slash ('/'), while a relative path doesn't.
To use the function, we first need to import it in our program as follows.
from os.path import isabs
isabs(path)
The function returns a boolean value, True
if the path is an absolute path and False
otherwise. It does not validate whether the path actually exists.
#import the function
from os.path import isabs
paths = ['/', '/temp/usr', 'images/me.jpg', '/home/files', 'desktop/project/test.py', '/users/john']
for p in paths:
if isabs(p):
print(p)
In the above example, we have a list of paths, we used a for loop to iterate over the paths calling the isabs()
function during each iteration. Only the absolute paths are printed.