Python os.path.abspath() Method



The abspath(path) method in the os.path module is used to get the normalized absolutized version of the provided pathname path.

The normalized absolutized version of a path is nothing but a modified string that represents a path or file so that it conforms to a valid path format on the target operating system. This involves resolving any symbolic links like "." and "..", and ensuring proper separation of directories using the correct path separator for the platform (/ on Unix-like systems and \ on Windows).

This method is particularly useful when you dealing with file paths to ensure consistency and accuracy in file operations.

Syntax

Following is the syntax of the method −

os.path.abspath(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 string representing the absolute version of the provided path.

Examples

Let's explore some examples to understand how the os.path.abspath() method works.

Example

The following example uses the os.path.abspath() method to get the full path of the current script(file).

# Import the os module
import os

# Getting the full path of the current script
result = os.path.abspath(__file__)
print(result)
print(type(result))

Output

On executing the above code in our online compiler will get the following output −

/home/cg/root/66260d93dc341/main.py
class 'str'>

Example

In this example, the os.path.abspath() method to get normalized path from the input path which contains the segment "..", usually this refers to the parent directory.

# Import the os module
import os 

# Getting the normalized absolute path
path = 'mydir/../myfile.txt'
normalized_path = os.path.abspath(path)

# Display the normalizedpath
print(normalized_path)

Output

On executing the above code in our online compiler will get the following output −

/home/cg/root/66260f4adb57d/myfile.txt

Example

In this example the path is provided to the os.path.abspath() method with the "." as a path segment to get the normalized path.

# Import the os module
import os 

# Getting the normalized absolute path
path = 'mydir/./myfile.txt'
normalized_path = os.path.abspath(path)
print(normalized_path)

Output

On executing the above code in our online compiler will get the following output −

/home/cg/root/66260f4adb57d/mydir/myfile.txt

Example

In the following example the os.path.abspath() method modifies the path containg double slashes "//" into a single slash "/" and retuns the absolute path to the file "myfile.txt" within the "mydir" directory.

# Import the os module
import os 

# Getting the normalized absolute path
path = 'mydir//myfile.txt'
normalized_path = os.path.abspath(path)
print(normalized_path)

Output

On executing the above code in our online compiler will get the following output −

/home/cg/root/66260f4adb57d/mydir/myfile.txt
os_path_methods.htm
Advertisements