Created
January 4, 2017 19:14
-
-
Save yunwilliamyu/8e76b391aa366cbdb77c231111ba2f52 to your computer and use it in GitHub Desktop.
Redirect Python stderr/stdout for a block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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