我已经用Python编写了一个脚本。它具有一个GUI,我打算分发给我的同事。如果他们在运行它时遇到错误,是否有办法将这些错误写入文本文件,这样我才能知道错误是什么?在Jupyter笔记本中,每当我在脚本中遇到错误时,它将显示为Traceback错误。每当有人发现错误时,我都希望在文本文件中看到此类信息。
谢谢
您可以尝试这样的事情
import time
import os
import sys
def logger(logfile, bolus):
# Takes two inputs - logfile (path to desired file), and data to be written
# Writes "Y-M-D|H:M:S, bolus\n"
f = open(logfile, 'a+')
currentdate = time.strftime('%Y-%m-%d|%H:%M:%S')
f.write(currentdate + ',' + data +'\n')
f.close()