Is shutil.get_terminal_size useless?

Chris Angelico rosuav at gmail.com
Sat Jan 28 03:27:20 EST 2017


On Sat, Jan 28, 2017 at 7:03 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> But if I pipe the output to something else, the shutil version fails to
> determine the correct terminal size, and falls back on the default:
>
>
> [steve at ando ~]$ python3.5 test_gts.py | cat
> shutil: os.terminal_size(columns=999, lines=999)
> os: os.terminal_size(columns=116, lines=29)
>
>
> while the os version gives the correct result.

I believe the problem here is your definition of "correct". When you
inquire of the os module, you're asking, at a fairly low level, what
the terminal window is sized to. But when you ask shutil, you're
asking what a shell utility should do. Quite a few programs change in
behaviour when piped into something else (for instance, 'ls' will
often apply colour and columnate its text, but if you pipe it into
grep, you don't want any of that), and shutil is acknowledging that
different effect.

Both are correct answers - to different questions.

ChrisA



More information about the Python-list mailing list