Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether the process is being run as a pipe

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.

like image 897
jackhab Avatar asked Nov 24 '10 09:11

jackhab


1 Answers

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.

like image 90
Matthew Flaschen Avatar answered Sep 28 '22 07:09

Matthew Flaschen