Open In App

os.path.abspath() method - Python

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The os.path.abspath() method in Python's os module is used to get the full (absolute) path of a file or folder. It's useful when you're working with relative paths but need to know the exact location on your system. If you pass a relative path, it converts it to an absolute one and if the path is already absolute, it returns it unchanged. Example:

Python
import os
a = "myfolder/myfile.txt" # Relative path
b = os.path.abspath(a)    # Get absolute path

print(b)

Output

Output
Using os.path.abspath()

Explanation: os.path.abspath() convert a relative file path to an absolute path by combining it with the current working directory, then prints the absolute path.

Syntax of os.path.abspath()

os.path.abspath(path)

Parameter: path is a string representing the relative or absolute path.

Returns: A string representing the absolute path.

Examples of using os.path.abspath()

Example 1: In this example, we are using the os.path.abspath() function to get the absolute path of the current script file.

Python
import os

p = os.path.abspath(__file__)   # Get absolute path
print(p)

Output

Output
Using os.path.abspath()

Explanation: __file__ attribute represents the script's relative path and os.path.abspath() converts it to the full absolute path by combining it with the current working directory.

Example 2: In this example, we are using the os.path.abspath() function with "." and ".." to get the absolute paths of the current and parent directories.

Python
import os

print("Current Directory:", os.path.abspath("."))
print("Parent Directory:", os.path.abspath(".."))

Output

Output
Using os.path.abspath()

Explanation: ." refers to the current directory, so os.path.abspath(".") gives its full path. ".." refers to the parent directory and os.path.abspath("..") returns its absolute path.

Example 3: In this example, we are combining a folder and file name using os.path.join() to create a relative path.

Python
import os
f, n = "data", "report.csv"  # Folder and file
a = os.path.abspath(os.path.join(f, n))  # Absolute path

print(a)

Output

Output
Using os.path.abspath()

Explanation: This code combines the folder "data" and file "report.csv" using os.path.join() to form a relative path. Then, os.path.abspath() converts that relative path into an absolute path, which is stored in variable a.


Next Article

Similar Reads