Advertisement
gauravssnl

save_stdout.py

Sep 28th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. # save_stdout.py
  2. # saves print statements to file instead of printing
  3. import sys
  4. file = "C:\\logszz.txt"
  5. print "Text1 is printed"
  6. # save normal stdout for later use
  7. saveout = sys.stdout
  8. # open file
  9. fsock = open(file,"w")
  10. # assin stdout to file object for writing/logging print statements to file without display
  11. sys.stdout =  fsock
  12. print "Text2 is not printed but written to file"
  13. # restore normal stdout
  14. sys.stdout = saveout
  15. # close file
  16. fsock.close()
  17. print "stdout is restored"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement