tags:

views:

452

answers:

4

I want a program to do one thing if executed like this:

cat something | my_program.py

and do another thing if run like this

my_program.py

But if I read from stdin, then it will wait for user input, so I want to see if there is anything to read before trying to read from stdin.

A: 

I do not know the Python commands off the top of my head, but you should be able to do something with poll or select to look for data ready to read on standard input.

That might be Unix OS specific and different on Windows Python.

Zan Lynx
Something like: select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []) And, yes, it is unix-specific.
John Fouhy
+13  A: 

If you want to detect if someone is piping data into your program, or running it interactively you can use isatty to see if stdin is a terminal:

$ python -c 'import sys; print sys.stdin.isatty()'
True
$ echo | python -c 'import sys; print sys.stdin.isatty()'
False
A: 

Bad news. From a Unix command-line perspective those two invocations of your program are identical.

Unix can't easily distinguish them. What you're asking for isn't really sensible, and you need to think of another way of using your program.

In the case where it's not in a pipeline, what's it supposed to read if it doesn't read stdin?

Is it supposed to launch a GUI? If so, you might want to have a "-i" (--interactive) option to indicate you want a GUI, not reading of stdin.

You can, sometimes, distinguish pipes from the console because the console device is "/dev/tty", but this is not portable.

S.Lott
+1: Anyone who has ever fought an application that tried cute tricks to get around this will appreciate any extra effort spent avoid such cute tricks (or at least making them explicit, as S.Lott suggests).
Jarret Hardie
Those invocations aren't identical, as "unknown (google)" pointed out using sys.stdin.isatty(). "isatty" is portable under Unix, and used for example in 'ls' (http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c ). But yes, it's a trick to be wary about using.
Andrew Dalke
The invocations are identical -- indistinguishable. The devices connected to stdin are slightly different. Depending on /dev/tty is workable, but an option is trivial and obvious.
S.Lott
Absolutely +1, I often use --stdin to tell the app to do this, lots of unixy programs just use -
Ali A
Hmmm. Okay, nuanced differences in how we use "invoke". I think of the invocation as including the "|" to use a pipe for stdin, while for you it's what's in the exec call. In any case, 'isatty' doesn't check for /dev/tty, it does fstat of stdin and checks if st_mode is/isn't a character special file
Andrew Dalke
The nuance IS the point -- one can try to depend on isatty and hope the user didn't do something screwy, or one can provide an explicit command-line option that says "--interactive" vs. the default assumption, which is read from stdin. No mystery. No obscure dependency on "isatty".
S.Lott
+5  A: 

You want the select module (man select on unix) It will allow you to test if there is anything readable on stdin. Note that select won't work on Window with file objects. But from your pipe-laden question I'm assuming you're on a unix based os :)

http://docs.python.org/library/select.html

root::2832 jobs:0 [~] # cat stdin_test.py
#!/usr/bin/env python
import sys
import select

r, w, x = select.select([sys.stdin], [], [], 0)
if r:
    print "READABLES:", r
else:
    print "no pipe"

root::2832 jobs:0 [~] # ./stdin_test.py
no pipe

root::2832 jobs:0 [~] # echo "foo" | ./stdin_test.py
READABLES: [<open file '<stdin>', mode 'r' at 0xb7d79020>]
Trey Stout
Andrew Dalke
Interesting note. I think that's just because the select is not blocking. When I change the 0 to a 5 your example properly shows the open file. It should really be put into a loop that could then cycle until data was available. Good point :)
Trey Stout