Python编程:traceback模块

废话不多说,直接上代码


import traceback

try:
    1/0
except:
    traceback.print_exc(file=open("tb.txt","w+"))  # except的缩写
else:
    print("success!")
finally:
    print("ok")  # ok

"""tb.txt
Traceback (most recent call last):
  File "D:\GitHub\SomeCodeForPython\test_from_myself\Python17个常用内置模块总结\16.traceback模块.py", line 4, in 
    1/0
ZeroDivisionError: division by zero
"""

你可能感兴趣的:(python)