如何检查stdin是否有数据?

时间:2021-09-15 21:08:04

In Python, how do you check if sys.stdin has data or not?

在Python中,如何检查系统系统。stdin是否有数据?

I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available.

我发现os.isatty(0)不仅可以检查stdin是否连接到TTY设备,还可以检查是否有数据可用。

But if someone uses code such as

但是如果有人使用诸如此类的代码。

sys.stdin = cStringIO.StringIO("ddd")

and after that uses os.isatty(0), it still returns True. What do I need to do to check if stdin has data?

在使用os.isatty(0)之后,它仍然返回True。我需要做什么来检查stdin是否有数据?

4 个解决方案

#1


52  

On Unix systems you can do the following:

在Unix系统上,您可以执行以下操作:

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.

在Windows上,select模块只能与套接字一起使用,因此需要使用另一种机制。

#2


41  

I've been using

我一直在使用

if not sys.stdin.isatty()

Here's an example:

这里有一个例子:

4 import sys
5
6 def main():
7     if not sys.stdin.isatty():
8         print "not sys.stdin.isatty"
9     else:
10         print "is  sys.stdin.isatty"

>echo "asdf" | stdin.py
not sys.stdin.isatty

sys.stdin.isatty() returns false if there's something in stdin.

isatty()返回false,如果stdin中有内容。

isatty(...)
    isatty() -> true or false. True if the file is connected to a tty device.

#3


1  

Depending on the goal here:

取决于这里的目标:

import fileinput
for line in fileinput.input():
    do_something(line)

can also be useful.

也可以是有用的。

#4


1  

As mentionned by others, there's no foolproof way to do it, because UNIX doesn't allow it. Always wait for stdin, even if there may be nothing (that's what grep etc. do), or ask the user for a - argument.

正如其他人提到的,没有万无一失的方法,因为UNIX不允许这样做。总是等待stdin,即使可能什么都没有(grep等做的),或者向用户请求一个-参数。

#1


52  

On Unix systems you can do the following:

在Unix系统上,您可以执行以下操作:

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.

在Windows上,select模块只能与套接字一起使用,因此需要使用另一种机制。

#2


41  

I've been using

我一直在使用

if not sys.stdin.isatty()

Here's an example:

这里有一个例子:

4 import sys
5
6 def main():
7     if not sys.stdin.isatty():
8         print "not sys.stdin.isatty"
9     else:
10         print "is  sys.stdin.isatty"

>echo "asdf" | stdin.py
not sys.stdin.isatty

sys.stdin.isatty() returns false if there's something in stdin.

isatty()返回false,如果stdin中有内容。

isatty(...)
    isatty() -> true or false. True if the file is connected to a tty device.

#3


1  

Depending on the goal here:

取决于这里的目标:

import fileinput
for line in fileinput.input():
    do_something(line)

can also be useful.

也可以是有用的。

#4


1  

As mentionned by others, there's no foolproof way to do it, because UNIX doesn't allow it. Always wait for stdin, even if there may be nothing (that's what grep etc. do), or ask the user for a - argument.

正如其他人提到的,没有万无一失的方法,因为UNIX不允许这样做。总是等待stdin,即使可能什么都没有(grep等做的),或者向用户请求一个-参数。