filename and line number of python script

前端 未结 9 1489
醉话见心
醉话见心 2020-11-28 01:50

How can I get the file name and line number in python script.

Exactly the file information we get from an exception traceback. In this case without raising an excep

9条回答
  •  佛祖请我去吃肉
    2020-11-28 02:47

    Thanks to mcandre, the answer is:

    #python3
    from inspect import currentframe, getframeinfo
    
    frameinfo = getframeinfo(currentframe())
    
    print(frameinfo.filename, frameinfo.lineno)
    

提交回复
热议问题