gpt4 book ai didi

Python:登录后无法删除文件

转载 作者:行者123 更新时间:2023-12-05 01:00:24 25 4
gpt4 key购买 nike

我正在使用以下代码写入日志文件:

import logging
from gmplot import gmplot
logging.basicConfig(filename="sample.log", level=logging.INFO)
logging.debug("This is a debug message")
logging.info("Informational message")
logging.error("An error has happened!")

但随后无法删除此文件。我怎样才能“释放”这个文件?

最佳答案

您需要关闭()您的日志记录:

正如那里解释的那样: python does not release filehandles to logfile

当你的 Run 类完成时,调用:

handlers = self.log.handlers[:]
for handler in handlers:
handler.close()
self.log.removeHandler(handler)

关于Python:登录后无法删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50215200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com