Advertisement
Oxyd

sys.stdout redirection func v.2

Jan 26th, 2020
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         out.flush()         #flush buffers
  8.     finally:
  9.         if outfile is not None: out.close()
  10. sprint('test')              #write to screen
  11. sprint('test', 'test.log')  #write to log
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement