Advertisement
Oxyd

sys.stdout redirection func

Jan 26th, 2020
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.36 KB | None | 0 0
  1. import sys
  2. def sprint(mytext, outfile=None):
  3.     if outfile is None: out = sys.stdout
  4.     else: out = open(outfile, 'w')
  5.     try:                    # do some stuff
  6.         out.write(mytext + '\n') # ...
  7.     finally:
  8.         if outfile is not None: out.close()
  9. sprint('test')              #write to screen
  10. sprint('test', 'test.log')  #write to log
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement