例如,是否有任何可以添加到下面的内容可以提供错误的行号?
try:
assert False
except Exception, e:
# lineOfError = ?
# print lineOfError
print e
答案 0 :(得分:1)
您可以使用traceback
模块:
from sys import exc_info
from traceback import extract_tb
try:
assert False
except Exception as e:
print(extract_tb(exc_info()[2])[0][1])
print(e)