How do I check if stdin has some data?
I've been using
if not sys.stdin.isatty()
Here's an example:
import sys
def main():
if not sys.stdin.isatty():
print "not sys.stdin.isatty"
else:
print "is sys.stdin.isatty"
Running
$ echo "asdf" | stdin.py
not sys.stdin.isatty
sys.stdin.isatty()
returns false if stdin
is not connected to an interactive input device (e.g. a tty).
isatty(...)
isatty() -> true or false. True if the file is connected to a tty device.
On Unix systems you can do the following:
import sys
import select
if select.select([sys.stdin, ], [], [], 0.0)[0]:
print("Have data!")
else:
print("No data")
On Windows the select module may only be used with sockets though so you'd need to use an alternative mechanism.