sys.stdout redirection func

Oxyd Jan 26th, 2020 85 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.     finally:
  8.         if outfile is not None: out.close()
  9. sprint('test')              #write to screen
  10. sprint('test', 'test.log')  #write to log
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand