Use the traceback module:import sysimport tracebacktry: assert True assert 7 == 7 assert 1 == 2 # many more statements like thisexcept AssertionError: _, _, tb = sys.exc_info() traceback.print_tb(tb) # Fixed format tb_info = traceback.extract_tb(tb) filename, line, func, text = tb_info[-1] print('An error occurred on line {} in statement {}'.format(line, text)) exit(1) 这篇关于如何在Python中处理AssertionError并找出发生在哪一行或语句上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 20:08