Python os.path.isabs() – Checks if the specified path is absolute
Python os.path.isabs()
The Python os.path.isabs() method is used to check if the specified path is absolute.
On Unix platforms, an absolute path begins with a forward slash (‘ / ‘), while on Windows, an absolute path begins with a backslash (‘ ‘) after removing any possible drive letter.
Syntax: os.path.isabs(path)
Parameters:
path: A path-like object representing a file system path.
A path-like object is a string or bytes object representing a path.
Return type: This method returns a bool-like value. If the specified path is absolute, this method returns True; otherwise, it returns False.
Example 1
Use the os.path.isabs() method (on Unix platforms)
# Python program to explain os.path.isabs() method
# importing os.path module
importos.path
#Path
path = '/home/User/Documents'
# Check whether the
#specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
#Path
path = 'home/User/Documents/'
# Check whether the
#specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
#Path
path = '../User/Documents/'
# Check whether the
#specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
Output:
True
False
False
Example 2
Using the os.path.isabs() method (on Windows)
# Python program to explain os.path.isabs() method
# importing os.path module
importos.path
#Path
path = r"C:UsersDocuments"
# Check whether the
#specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
#Path
path = r"UserDocuments"
# Check whether the
#specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
#Path
path = r"UserDocuments"
# Check whether the
#specified path is an
# absolute path or not
isabs = os.path.isabs(path)
print(isabs)
Output:
True
True
False