Get Absolute Path of a File in Python
To get the absolute path of a file in Python, you can use the os.path.abspath()
function from the os
module or the Path.resolve()
method from the pathlib
module. These methods allow you to retrieve the full path of a file, ensuring clarity when working with relative file paths.
Examples to Get Absolute Path
1. Using os.path.abspath()
to Get Absolute Path
In this example, we will use the os.path.abspath()
function to get the absolute path of a file. This function takes a relative path and returns the full absolute path.
main.py
import os
# Define a relative file path
relative_path = "example.txt"
# Get the absolute path
absolute_path = os.path.abspath(relative_path)
# Print the absolute path
print("Absolute Path:", absolute_path)
Explanation:
- We import the
os
module to access file path functions. - We define a relative file path as
"example.txt"
. - We use
os.path.abspath(relative_path)
to convert the relative path into an absolute path. - We print the absolute path using the
print()
function.
Output:

2. Using pathlib.Path.resolve()
to Get Absolute Path
We will use the pathlib.Path.resolve()
method to obtain the absolute path of a file. The pathlib
module provides an object-oriented approach to working with file paths.
main.py
from pathlib import Path
# Define a relative file path
relative_path = Path("example.txt")
# Get the absolute path
absolute_path = relative_path.resolve()
# Print the absolute path
print("Absolute Path:", absolute_path)
Explanation:
- We import
Path
from thepathlib
module. - We create a
Path
object for the file"example.txt"
. - We use
relative_path.resolve()
to get the absolute path. - We print the absolute path using the
print()
function.
Output:

3. Getting Absolute Path of a Script File
In this example, we will retrieve the absolute path of the currently running script using os.path.abspath()
and __file__
.
main.py
import os
# Get the absolute path of the script
script_path = os.path.abspath(__file__)
# Print the absolute path
print("Script Absolute Path:", script_path)
Explanation:
- We import the
os
module. - We use
os.path.abspath(__file__)
to get the absolute path of the currently executing script. - We print the script’s absolute path using
print()
.
Output:

4. Getting Absolute Path of the Current Working Directory
In this example, we will find the absolute path of the current working directory using os.getcwd()
.
main.py
import os
# Get the absolute path of the current working directory
cwd = os.getcwd()
# Print the absolute path
print("Current Working Directory:", cwd)
Explanation:
- We import the
os
module. - We use
os.getcwd()
to get the absolute path of the current working directory. - We print the current working directory path using
print()
.
Output:

Conclusion
In summary, here are the different ways to get the absolute path of a file in Python:
os.path.abspath()
: Converts a relative path to an absolute path.pathlib.Path.resolve()
: Object-oriented approach to getting absolute paths.os.path.abspath(__file__)
: Retrieves the absolute path of the current script.os.getcwd()
: Gets the absolute path of the current working directory.