技巧:Python中print打印信息的同时打印文件、行号

1 import sys
2 
3 def Log(msg):
4     print('Print Message: '+msg+' ,File: "'+__file__+'", Line '+str(sys._getframe().f_lineno)+' , in '+sys._getframe().f_code.co_name)
5 
6 if __name__ == '__main__':
7     Log('hello') # Print Message: hello ,File: "i.py", Line 4 , in Log

Log 函数是对 print 打印的再封装,可以显示出错程序所在的脚本名称、行号以及函数名。


其中,

  • sys._getframe().f_lineno :当前行号,int
  • sys._getframe().f_code.co_name :当前文件名称 string

函数执行如下:

Print Message: hello ,File: "i.py", Line 4 , in Log