Skip to content

Instantly share code, notes, and snippets.

@sefgit
Forked from djsmith42/gist:3956189
Created November 23, 2024 03:37
Show Gist options
  • Save sefgit/5310aacf76d24f5c588b48aab50e45c4 to your computer and use it in GitHub Desktop.
Save sefgit/5310aacf76d24f5c588b48aab50e45c4 to your computer and use it in GitHub Desktop.
from contextlib import contextmanager
import sys, os
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
@sefgit
Copy link
Author

sefgit commented Nov 23, 2024

print "You can see this"
with suppress_stdout():
print "You cannot see this"
print "And you can see this again"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment