bnopne
bnopne

Reputation: 95

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?

Upvotes: 2

Views: 1006

Answers (2)

Kenly
Kenly

Reputation: 26738

If you want to flush stdout just add this line in your code.

sys.stdout.flush()

Upvotes: 0

blueFast
blueFast

Reputation: 44431

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.

Upvotes: 1

Related Questions