redirect prints to log file

Python lets you capture and assign sys.stdout – as mentioned – to do this:

import sys
old_stdout = sys.stdout

log_file = open("message.log","w")

sys.stdout = log_file

print "this will be written to message.log"

sys.stdout = old_stdout

log_file.close()

Leave a Comment