← 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)


pathlib files


🐍 You might also find interesting: