What's the best way to tell if a Python program has anything to read from stdin?
Asked Answered
A

4

40

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.

Antagonize answered 30/3, 2009 at 22:58 Comment(1)
What does "shouldn't be as hard as it seems" mean? Would you mind updating the question with a description of your goal, any code you've tried so far that hasn't worked, and what you expected to have happened?Radioelement
T
73

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
Tower answered 30/3, 2009 at 22:58 Comment(2)
If the script could be executed remotely, does this isatty() approach still work ? For example ssh somehost.com ' bash -c " python -c \"import sys; print sys.stdin.isatty()\" " ' returns FalseAlmsman
Using @Trey Stout 's answer the script behaves as expected over ssh. ssh somehost.com ' bash -c " python -c \"import sys; import select; r, w, x = select.select([sys.stdin], [], [], 0); print(r) \" " ' Prints []. The same command with pipe is: ssh somehost.com ' bash -c " echo something | python -c \"import sys; import select; r, w, x = select.select([sys.stdin], [], [], 0); print(r) \" " ' and that prints [<open file '<stdin>', mode 'r' at 0x7ff2aad250c0>]Almsman
L
10

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>]
Lesley answered 30/3, 2009 at 22:58 Comment(3)
This does not tell you if input is a pipe, only if input is available. Consider: ( sleep 1 && echo "foo" ) | ./stdin_test.py On my machine this reports "no pipe".Whitener
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 :)Lesley
Do you know how I can do sth similar to this in bash or zsh?Sangria
R
3

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.

Rewrite answered 30/3, 2009 at 22:58 Comment(7)
+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).Radioelement
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' (git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c ). But yes, it's a trick to be wary about using.Whitener
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.Rewrite
Absolutely +1, I often use --stdin to tell the app to do this, lots of unixy programs just use -Fusspot
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 fileWhitener
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".Rewrite
If you run grep -R stuff (forgetting to include at least one path), some greps will tell you "warning: recursive search of stdin" or something similar. I think that's a good use of TTY detection. Apparently GNU grep 2.12 goes one step further, if it detects the -R flag on stdin, it will search the current working directory instead of stdin.Prophesy
A
-3

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.

Augustaaugustan answered 30/3, 2009 at 22:58 Comment(1)
Something like: select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []) And, yes, it is unix-specific.Pentothal

© 2022 - 2024 — McMap. All rights reserved.