Redirecting stdout and stderr Output : stdout stderr « Development « Python






Redirecting stdout and stderr Output

Redirecting stdout and stderr Output

import sys 

print 'Dive in'                                   
saveout = sys.stdout                              
fsock = open('out.log', 'w')                      
sys.stdout = fsock                                
print 'This message will be logged instead of displayed'        
sys.stdout = saveout                                            
fsock.close()                                                   

           
       








Related examples in the same category

1.Use system function to outputUse system function to output
2.Different way to output string: print and sys.stdout.writeDifferent way to output string: print and sys.stdout.write
3.Introducing stdout and stderrIntroducing stdout and stderr
4.Redirecting Error Information
5.Printing to stderrPrinting to stderr