Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # save_stdout.py
- # saves print statements to file instead of printing
- import sys
- file = "C:\\logszz.txt"
- print "Text1 is printed"
- # save normal stdout for later use
- saveout = sys.stdout
- # open file
- fsock = open(file,"w")
- # assin stdout to file object for writing/logging print statements to file without display
- sys.stdout = fsock
- print "Text2 is not printed but written to file"
- # restore normal stdout
- sys.stdout = saveout
- # close file
- fsock.close()
- print "stdout is restored"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement