filename and line number of python script

前端 未结 9 1479
醉话见心
醉话见心 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:42

    Handy if used in a common file - prints file name, line number and function of the caller:

    import inspect
    def getLineInfo():
        print(inspect.stack()[1][1],":",inspect.stack()[1][2],":",
              inspect.stack()[1][3])
    

提交回复
热议问题