python捕获异常时打印文件名和行号 - xd21303/Notebook GitHub Wiki

try:
    print(a)
except NameError as e:
    print('发生错误的文件:', e.__traceback__.tb_frame.f_globals['__file__'])
    print('错误所在的行号:', e.__traceback__.tb_lineno)
    print('错误信息', e)

print('测试完毕')