Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the caller's filename, method name in python

Tags:

python

for example, a.boo method calls b.foo method. In b.foo method, how can I get a's file name (I don't want to pass __file__ to b.foo method)...

like image 805
Zhenyu Li Avatar asked Dec 04 '12 09:12

Zhenyu Li


People also ask

How do I get the name of a file object in Python?

To get the filename without extension in python, we will import the os module, and then we can use the method os. path. splitext() for getting the name. After writing the above code (Python get filename without extension), Ones you will print “f_name” then the output will appear as a “ file ”.

What is __ name __ Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.


1 Answers

You can use the inspect module to achieve this:

frame = inspect.stack()[1] module = inspect.getmodule(frame[0]) filename = module.__file__ 
like image 177
ThiefMaster Avatar answered Oct 03 '22 06:10

ThiefMaster