I have a small Python utility which should be run only as a pipe. I want it to print out the help message when it runs stand alone. How can a process know whether it is being used as a pipe. Comparing sys.stdin
and sys.__stdin__
does not work.
Asked
Active
Viewed 2,120 times
1 Answers
16
You can use isatty
:
if sys.stdin.isatty():
It will be True
if standard input is a tty, which roughly means it's being used directly, outside a pipe.

Matthew Flaschen
- 278,309
- 50
- 514
- 539
-
2Also `if sys.stdout.isatty()` for the opposite. – isaaclw Jul 30 '14 at 18:36
-
1Just a hint: if you start python in a CI environment ala travis, jenkins or gitlab-ci, it might be that `sys.stdin.isatty()` is always `False`. – Mirko Friedenhagen Feb 02 '18 at 12:28