Python:登录后无法删除文件

时间:2018-05-07 13:17:29

标签: python

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

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!")

但是,删除此文件是不可能的。我怎样才能发布'这个档案?

2 个答案:

答案 0 :(得分:3)

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

如下所述: python does not release filehandles to logfile

当您的Run类完成时,请调用:

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

答案 1 :(得分:1)

在完成日志记录后,只需使用 logging.shutdown()。

相关问题