Advertisement
Oxyd

sys.stdout redirection

Jan 26th, 2020
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import sys
  2. temp = sys.stdout                 # store original stdout object for later
  3. sys.stdout = open('log.txt', 'w') # redirect all prints to this log file
  4. print("testing123")               # nothing appears at interactive prompt
  5. print("another line")             # again nothing appears. it's written to log file instead
  6. sys.stdout.close()                # ordinary file object
  7. sys.stdout = temp                 # restore print commands to interactive prompt
  8. print("back to normal")           # this shows up in the interactive prompt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement