Get file directory from function file python

Viewed 30

Say my directory looks like this

folder
⮑ file.py
⮑ class.py

In want to make a function in class.py that can return the filepath of the current file when it is executed in file.py. I have tried __file__ and os.path.basename/dirname/abspath but they return the filepath of class.py when I want the filepath of file.py.

How can I get the filepath of file.py from class.py's function?

1 Answers

Just put the following in your function in class.py.

import inspect
filename = inspect.stack()[1].filename
Related