python中异常的保存及断言(用到traceback,assert)

"""
异常信息保存到文件中
"""
import traceback

try:
    raise Exception('This is the error message.')
except:
    errorFile = open('errorInfo.txt', 'w')
    errorFile.write(traceback.format_exc())
    errorFile.close()
    print('The traceback info was written to errorInfo.txt')

"""
断言  assert 条件,条件为False是显示的字符串
"""
a = 'open'

assert a == '123', 'a != 123'
posted @ 2022-02-19 10:06  fly_bk  阅读(123)  评论(0编辑  收藏  举报