print to screen and file with one print statement

Mike Müller mmueller at dgfz.de
Wed Feb 12 15:46:32 EST 2003


I would like to send my print statements to the terminal and to a log
file at the same time. I can use redirect as describe in Dive into
Python:

import sys

print 'Dive in'                                          
saveout = sys.stdout                                     
fsock = open('out.log', 'w')
sys.stdout = fsock                                 
print 'This message will be logged but not displayed' 
print 'This message should be logged and also displayed'
sys.stdout = saveout                                     
fsock.close()    

This redirects all output from Python to the open file.
At the same time I'd like to see all printed text on screen.
How can I do this?

Thanks 

Mike




More information about the Python-list mailing list