Part of the series

Several example codes

~1 min read • Updated Sep 30, 2025

Program Overview

This Python program displays the full path of the file that is currently being executed.
It uses the built-in variable __file__ along with functions from the os module to extract the absolute path.


Python Code:


import os

# Get the full path of the current file
current_path = os.path.abspath(__file__)
print("Current file path:", current_path)

Important Note:

In environments like Jupyter Notebook or interactive Python shells, the variable __file__ may not be defined.
To ensure proper output, save the code in a .py file and run it from a terminal or command line.


Sample Output:


Current file path: /home/shahin/projects/my_script.py

Technical Explanation:

- __file__ provides the relative path of the current script
- os.path.abspath() converts it to an absolute path
- This method is useful for locating the script within a project or filesystem


Written & researched by Dr. Shahin Siami