__file__Easy Examples

The file path from which a module was loaded

Accessing __file__

How to access and use __file__.

python
# __file__ holds the path of the current module
import os
print(os.__file__)

import json
print(json.__file__)

__file__ is a special attribute that the file path from which a module was loaded.

__file__ in practice

Using __file__ in everyday code.

python
# More __file__ examples
print("__file__ is a special Python attribute")

# Every object has certain dunder attributes
obj = object()
attrs = [a for a in dir(obj) if a.startswith("__")]
print(f"object has {len(attrs)} dunder attributes")

Understanding __file__ helps you introspect and debug Python objects.

Want to try these examples interactively?

Open Easy Playground