Xavier Olive research teaching python blog cli

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