Skip to content

Instantly share code, notes, and snippets.

@ruxi
Forked from djsmith42/gist:3956189
Last active March 21, 2017 20:47
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 ruxi/c5340bcf10432db63ddf6a79afbdd904 to your computer and use it in GitHub Desktop.
Save ruxi/c5340bcf10432db63ddf6a79afbdd904 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
import sys, os
@contextmanager
def suppress_stdout():
"""
source: https://gist.github.com/djsmith42/3956189
usage:
print "You can see this"
with suppress_stdout():
print "You cannot see this"
print "And you can see this again"
"""
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment