filename and line number of python script

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

    In Python 3 you can use a variation on:

    def Deb(msg = None):
      print(f"Debug {sys._getframe().f_back.f_lineno}: {msg if msg is not None else ''}")
    

    In code, you can then use:

    Deb("Some useful information")
    Deb()
    

    To produce:

    123: Some useful information
    124:
    

    Where the 123 and 124 are the lines that the calls are made from.

提交回复
热议问题