20 – Dunder attribute __file__

20 – Dunder attribute __file__#

The dunder attribute __file__ can be used to get the full path of your Python script or module.

This can be useful, for example, to locate a resources folder that is “next” to your code in your project directory:

from pathlib import Path

print(__file__)
# /Users/rodrigogs/Documents/my_project/example.py

RESOURCES = (Path(__file__).parent / "res").resolve()
print(RESOURCES)
# /Users/rodrigogs/Documents/my_project/res