Python : clear a log file

烈酒焚心 提交于 2019-12-03 16:06:48

问题


I develop a client-server application and I have log in the server, so I use the logging module. I would like to create a command in the server to clear the file.

I have test with os.remove() but after, the log doesn't work.

Do you have an idea?

Thanks.


回答1:


It might be better to truncate the file instead of removing it. The easiest solution is to reopen the file for writing from your clearing function and close it:

with open('yourlog.log', 'w'):
    pass


来源:https://stackoverflow.com/questions/8898997/python-clear-a-log-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!