Redirect both stdout and stderr with python tqdm library

爱⌒轻易说出口 提交于 2019-12-11 02:22:42

问题


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?


回答1:


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'


来源:https://stackoverflow.com/questions/45722952/redirect-both-stdout-and-stderr-with-python-tqdm-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!