trackback 模块
【示例】使用 Traceback 模块打印异常信息
#coding=utf-8
import traceback
try:
print("step1")
num = 1/0
except:
traceback.print_exc()
运行结果:
运行结果:
step1
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/mypro_exception/my01.py", line 7, in <module>
num = 1/0
ZeroDivisionError: division by zero
Process finished with exit code 0
#【示例】使用 traceback 将异常信息写入日志文件
#coding=utf-8
import traceback
try:
print("step1")
num = 1/0
except:
with open("d:/a.log","a") as f:
traceback.print_exc(file=f)
运行结果:
"D:\Program Files\Python310\python.exe" D:\work\python\four\mypy10.py
step1
进程已结束,退出代码0
标签:模块,traceback,写入,num,step1,print,日志 From: https://www.cnblogs.com/yuanzijian/p/16912291.html