How to print full path of current file's directory in Python?

The path of a current file is defined with the help of directory hierarchy; and it contains the backtracked path from the current file to the root directory this file is present in. For instance, consider a file "my_file" belongs to a directory "my_directory", the path for this file is defined as given below ?

./my_directory/my_file

The directory, sub-directory and the file are all separated using the "/" separator in the path.

Therefore, to get current file's full path, you can use the os.path.abspath() function. If you want only the directory path, you can call os.path.dirname() method.

Using os.path.abspath() Method

The os.path.abspath() function accepts the path as an argument and is used to get the complete normalized absolute path of the current file.

Example

The following example demonstrates how to retrieve the absolute path of the current script file ?

import os

# Get the absolute path of the current script
current_file = __file__
print("Current file:", current_file)
print("Absolute path:", os.path.abspath(current_file))
Current file: /tmp/script.py
Absolute path: /tmp/script.py

Using os.path.dirname() Method

The os.path.dirname() function is used to get the directory path (without the file name) of the current file.

This method only accepts a string or a byte as an argument; however, one must pass the complete path of the current file to retrieve the path of its current directory.

This relationship can be summarized as ?

os.path.dirname(path) + filename = complete absolute path

Example

Here, we will first obtain the complete path of the current file using __file__, then pass it to the dirname() method to get the directory path ?

import os

# Get current file's directory
current_file = __file__
absolute_path = os.path.abspath(current_file)
directory_path = os.path.dirname(absolute_path)

print("Current file:", current_file)
print("Absolute path:", absolute_path)
print("Directory path:", directory_path)
Current file: /tmp/script.py
Absolute path: /tmp/script.py
Directory path: /tmp

Using pathlib Module (Modern Approach)

Python 3.4+ provides the pathlib module which offers a more object-oriented approach to file path operations ?

from pathlib import Path

# Get current file path using pathlib
current_file = Path(__file__)
print("Current file:", current_file)
print("Absolute path:", current_file.absolute())
print("Directory path:", current_file.parent)
Current file: script.py
Absolute path: /tmp/script.py
Directory path: /tmp

Comparison

Method Get Absolute Path Get Directory Python Version
os.path os.path.abspath(__file__) os.path.dirname(os.path.abspath(__file__)) All versions
pathlib Path(__file__).absolute() Path(__file__).parent 3.4+

Conclusion

Use os.path.abspath(__file__) to get the current file's absolute path and os.path.dirname() to get just the directory. For modern Python, consider using pathlib.Path for cleaner, object-oriented path operations.

Updated on: 2026-03-24T18:25:13+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements