Home >Backend Development >Python Tutorial >Print current python file error line

Print current python file error line

高洛峰
高洛峰Original
2016-10-19 14:56:201565browse

During the python error debugging process, it is sometimes necessary to print the error line of the current python file. Here is a method:

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)

Print the error line of execfile:

try ; 1][1])

Use the reflection mechanism to call the function and print the callee’s error line and error message

try:

callfunc.callfunc(myklass,strmethod,params)

except :

print '= == STEP ERROR Info Start'

Import Traceback

Traceback.print_exc ()

Print '=== Step Error Info End'

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn