Ainz Titor
Ainz Titor

Reputation: 1557

Redirect both stdout and stderr with python tqdm library

I'm using tqdm in Python to display console progress bars.

I have a function from another library that occasionally writes to both stdout and stderr inside the tqdm loop. I cannot hack the source code of that function.

While this doc shows how to redirect sys.stdout, it doesn't easily generalize to stderr because I can pass only one of stdout or stderr to the file= parameter in tqdm's __init__. Please refer to this question and its accepted answer for the minimal code that illustrates the problem.

How do I redirect both stdout and stderr together?

Upvotes: 2

Views: 4358

Answers (1)

wim
wim

Reputation: 363233

Python provides some helpers for you in the standard libraries, look in contextlib:

>>> import io, sys
>>> from contextlib import redirect_stdout, redirect_stderr
>>> from tqdm import tqdm
>>> def foo():
...     print('spam to stdout')
...     print('spam to stderr', file=sys.stderr)
...     
>>> out = io.StringIO()
>>> err = io.StringIO()
>>> with redirect_stdout(out), redirect_stderr(err):
...     for x in tqdm(range(3), file=sys.__stdout__):
...         print(x)  # more spam
...         foo()
...         
100%|██████████| 3/3 [00:00<00:00, 29330.80it/s]
>>> out.getvalue()
'0\nspam to stdout\n1\nspam to stdout\n2\nspam to stdout\n'
>>> err.getvalue()
'spam to stderr\nspam to stderr\nspam to stderr\n'

Upvotes: 5

Related Questions