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

后端 未结 6 1522
别那么骄傲
别那么骄傲 2020-12-05 13:46

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__

6条回答
  •  不知归路
    2020-12-05 14:24

    You can use the inspect module to achieve this:

    frame = inspect.stack()[1]
    module = inspect.getmodule(frame[0])
    filename = module.__file__
    

提交回复
热议问题