Why is tqdm output directed to sys.stderr and not to sys.stdout?
Asked Answered
B

1

8

Accordingo to the python python documentation concerning sys.stdout and sys.stderr:

stdout is used for the output of print() and expression statements and for the prompts of input();

The interpreter’s own prompts and its error messages go to stderr.

Nevertheless, according to the documentation ot tqdm the default output is sys.stderr.

I am confused on why this would be the case given that it does not seem to be related to the interpreter own prompts or error messages. What am I missing? Why is tqdm output directed to sys.stderr and not to sys.stdout?

Edit: I think that the discussion here sort of answers this: When to use sys.stdout instead of sys.stderr?

Banter answered 27/2, 2023 at 12:39 Comment(0)
S
7

Redirecting progress bar to sys.stderr makes tqdm more powerful in command line. For example, consider this command

$ time find . -name '*.py' -type f -exec cat \{} \; | tqdm | wc -l

Simply inserting tqdm(or python -m tqdm) between pipes will pass through all stdin to stdout while printing progress to stderr. This makes piping easier because tqdm does not alter the stdout of one command allowing us to use wc -l in the same chain.

Schweinfurt answered 27/2, 2023 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.