在python錯誤除錯過程中有時需要列印當前python檔案錯誤行,下面來介紹一下方法:
import sys
try:
a = [1,2]
print a[3]
except:
s=sys.exc_info()
print "Error '%s' happened on line %d" % (s[1],s[2].tb_lineno)
列印execfile的列印錯誤行:
try:
execfile("tprint.py")
except Exception, info:
#print info[1]
print "Error '%s' happened on line %d" % (info[0], info[1][1])
利用反射機制,呼叫函式,列印被呼叫方的錯誤行及錯誤資訊
try:
callfunc.callfunc(myklass,strmethod,params)
except :
print '=== STEP ERROR INFO START'
import traceback
traceback.print_exc()
print '=== STEP ERROR INFO END'