'Python clear stdout
I have some kind of web server. It writes log to sys.stdout which was redirected to a file. I need to periodically clear this file as it grows. Can I do it using only sys.stdout?
Solution 1:[1]
What you can do is to delete the logfile:
rm logfile
or truncate it:
echo -n > logfile
The webserver will still use the open filedescriptor, and the file will still be on disk, but not reachable by that filename anymore. Once the server restarts (with the same redirection), the logfile will be created from scratch.
Another option would be to use logrotate for your logs, which allows you to change the logfile you are writing to.
Solution 2:[2]
If you want to flush stdout just add this line in your code.
sys.stdout.flush()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
