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.
import os
# Get the full path of the current file
current_path = os.path.abspath(__file__)
print("Current file path:", current_path)
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.
Current file path: /home/shahin/projects/my_script.py
- __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