Get file directory from function file python

Question:

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?

Asked By: hbzy

||

Answers:

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

import inspect
filename = inspect.stack()[1].filename
Answered By: GooJ
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.