← Go Back
How to Get Path and Name of the Current Script
Using the__file__
object automatically defined by Python:import pathlib
# Get the absolute path of the current file.
p = pathlib.Path(__file__).absolute()
# File name (e.g. hello.py.)
print(p.name)
# Path without file name, e.g. C:\Users\Documents
# or /home/user/Desktop.
print(p.parent)
# Path + file name.
print(p)
🐍 You might also find interesting: