请问怎样把Python运行时系统运行的错误信息保存到一个txt文件中
比如我运行3除以0是返回的错误信息,把下面打印的这段保存起来,该怎么做>>>Traceback(mostrecentcalllast):File"C:\Users\len...
比如我运行3除以0是返回的错误信息,把下面打印的这段保存起来,该怎么做
>>>
Traceback (most recent call last):
File "C:\Users\len\Desktop\new.py", line 1, in <module>
3/0
ZeroDivisionError: division by zero
>>> 展开
>>>
Traceback (most recent call last):
File "C:\Users\len\Desktop\new.py", line 1, in <module>
3/0
ZeroDivisionError: division by zero
>>> 展开
展开全部
#!/usr/bin/env python3
def main():
fname = 'errors.txt'
a, b = 3, 0
try:
c = a / b
except Exception as e:
print(repr(e))
with open(fname, 'w') as f:
f.write(repr(e))
print('save error message to {}'.format(fname))
else:
print(c)
if __name__ == '__main__':
main()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询