Python os.path.isabs() Method
The isabs(path) method in the os.path module is used to determine whether a given pathname path is an absolute pathname or not. An absolute pathname is nothing but a valid pathname that refers to a file or directory on the target operating system. On Unix-based systems, an absolute pathname begins with a forward slash (/), while on Windows, it typically begins with a drive letter followed by a backslash (\).
This method can be useful when working with file paths to ensure that the provided path is valid path or not, especially when dealing with paths from different operating systems.
Syntax
Following is the syntax of the method −
os.path.isabs(path)
Parameters
Here are the details of its parameters −
- path: This parameter represents a path-like object, which can be either a string or bytes object representing a file system path, or an object implementing the 'os.PathLike' protocol.
Return Value
The method returns a boolean value, where True represents the provided pathname path is an absolute pathname. And Flase representing the provided path is a relative pathname.
Examples
Let's explore some examples to understand how the os.path.isabs() method works.
Example
The following example uses the os.path.isabs() method to determine the given pathname 'D:/MyFile.txt' is an absolute pathname or a relative pathname.
# Import the os module
import os
# Define the path
path = 'D:/MyFile.txt'
# Check if the path is absolute
is_absolute = os.path.isabs(path)
# Print the result
if is_absolute:
print('The given path is an absolute pathname:', path)
else:
print('The given path is a relative pathname:', path)
Output
On executing the above code you will get the following output −
The given path is an absolute pathname: D:/MyFile.txt
The above output may vary based the operating system.
Example
In this example, the following pathname "mydir/../myfile.txt" is given to the os.path.isabs() method, to determine whether it is an absolute pathname or relative pathname.
import os
# Define the path
path = 'mydir/../myfile.txt'
# Check if the path is absolute
is_absolute = os.path.isabs(path)
# Print the result
if is_absolute:
print(f'The given "{path}" is an absolute pathname.', )
else:
print(f'The given "{path}" is a relative pathname.')
Output
On executing the above code you will get the following output −
The given "mydir/../myfile.txt" is a relative pathname.
Example
This example demonstrates the use of os.path.isabs() method to check whether the normalized absolute pathname is absolute or relative.
# Import the os module
import os
# Normalizing a path
path = 'mydir/./myfile.txt'
normalized_abs_path = os.path.abspath(path)
# Check if the path is absolute
is_absolute = os.path.isabs(normalized_abs_path)
# Print the result
if is_absolute:
print(f'The given "{normalized_abs_path}" is an absolute pathname.', )
else:
print(f'The given "{normalized_abs_path}" is a relative pathname.')
Output
On executing the above code in our online compiler will get the following output −
The given "/home/cg/root/66260f4adb57d/mydir/myfile.txt" is an absolute pathname.