Xavier Olive research teaching python blog til cli

How to detect if sys.stdout is attached to a terminal or piped

30 May 2022

$ python3 -c "import sys; print(sys.stdout.isatty())"
True
$ python3 -c "import sys; print(sys.stdout.isatty())" | cat
False