import traceback
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
try:
a=1+"2"
except Exception as e:
error_info = traceback.format_exc()
with open('1.txt', 'a', encoding='utf-8') as f:
f.write(current_time+' 错误信息为:' + error_info+'\n')
print('end')
执行后,报错信息保存在文件中: