Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Last active June 24, 2025 22:16
Show Gist options
  • Save dmyersturnbull/c0717406eb46158401a9 to your computer and use it in GitHub Desktop.
Save dmyersturnbull/c0717406eb46158401a9 to your computer and use it in GitHub Desktop.
Silence stdout and/or stderr in Python 3 using with statement
import contextlib
import sys
from io import StringIO
@contextlib.contextmanager
def silenced(no_stdout=True, no_stderr=True):
"""
Suppresses output to stdout and/or stderr.
Always resets stdout and stderr, even on an exception.
Usage:
with silenced(): print("This doesn't print")
Modified from post by Alex Martelli in https://stackoverflow.com/questions/2828953/silence-the-stdout-of-a-function-in-python-without-trashing-sys-stdout-and-resto/2829036#2829036
which is licensed under CC BY-SA 3.0 https://creativecommons.org/licenses/by-sa/3.0/
"""
if no_stdout:
save_stdout = sys.stdout
sys.stdout = StringIO()
if no_stderr:
save_stderr = sys.stderr
sys.stderr = StringIO()
yield
if no_stdout:
sys.stdout = save_stdout
if no_stderr:
sys.stderr = save_stderr
@bmeyers
Copy link

bmeyers commented Jun 24, 2025

thanks, this was helpful!

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