This Python program displays the name of the file that is currently being executed.
It uses the built-in variable __file__ to access the filename of the running script.
# Display the name of the current file
print("Current filename:", __file__)
If you run this code in environments like Jupyter Notebook or interactive 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 Python interpreter.
Current filename: my_script.py
- __file__ is a built-in Python variable that holds the path of the current script
- You can use the os module to extract just the filename or full path
import os
# Display only the filename without path
filename = os.path.basename(__file__)
print("Current filename:", filename)