Python print file execution path and line number

foreword

I am learning Python recently, but I feel that if the dynamic language does not print the file path and line number, if there is a problem Debugging, unless you are very familiar with the business, you will not be able to find the location of the problem.

Anyway, it's not bad to print

code part

import sys 

def ShowMsg(msg):
    print (msg,'     ',sys._getframe(1).f_code.co_filename,sys._getframe(1).f_lineno)

In fact, it is not difficult, python is suitable for this small function, small but strong.

Tips: Here is the location where the function is executed

demo

insert image description here

# 引用
import Utils.Utils as Utils

Utils.ShowMsg('我来打印')

insert image description here

Results of the

我来打印       D:\workSpace\python\train\test.py 13

insert image description here

Unexpected surprise, the file location jumps

insert image description here

Guess you like

Origin blog.csdn.net/qq_44695769/article/details/131675495