Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Forked from yunwilliamyu/process_wrapper.py
Created September 17, 2019 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GenevieveBuckley/3ba30e42f82ee79041aecd18883246f5 to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/3ba30e42f82ee79041aecd18883246f5 to your computer and use it in GitHub Desktop.
Redirect Python stderr/stdout for a block
import sys
import contextlib
@contextlib.contextmanager
def output_wrapper():
save_stdout = sys.stdout
save_stderr = sys.stderr
sys.stdout = open('stdout.log', 'a')
sys.stderr = open('stderr.log', 'a')
yield
sys.stdout = save_stdout
sys.stderr = save_stderr
with output_wrapper():
printf("Hello log!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment