Python在日志配置文件中设置filemode

我正在尝试使用
Python在文本配置文件中配置记录器.以下是部分内容:

[logger_root]
handlers=result
level=NOTSET

[handler_result]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=simple
level=DEBUG
args=('result_log.txt')

我想每次运行系统时都重写日志文件.但不知道如何在文件中设置它.我尝试了但是失败了:

args=('result_log.txt',filemode='w')

很多文章都讨论了如何从Python代码中设置它.但是我想在文件中设置它.怎么做?谢谢.

=======================================

编辑:我可以通过以下方式将一些信息写入日志文件’result_log.txt’:

result_logger = logging.getLogger('result')
result_logger.debug("info to write")

但我只能“追加”到该文件.我想每次运行时都重写文件.

最佳答案 您可以使用以下两种方法之一: –

>备份旧日志文件并使用RotatingFileHandler创建新文件
>替换文件本身(如果您不再需要旧日志).您可以查看步骤的this

希望这可以帮助.

点赞