判断 Python 程序是否可以从 stdin 读取任何内容的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/699390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:40:43  来源:igfitidea点击:

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

python

提问by Josh Gibson

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.

但是如果我从 stdin 读取,那么它将等待用户输入,所以我想在尝试从 stdin 读取之前查看是否有任何要读取的内容。

回答by brian-brazil

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:

如果您想检测是否有人将数据传输到您的程序中,或以交互方式运行它,您可以使用 isatty 来查看 stdin 是否为终端:

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

回答by Trey Stout

You want the select module (man selecton 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 :)

您需要 select 模块(man select在 unix 上)它将允许您测试 stdin 上是否有任何可读内容。请注意, select 不适用于带有文件对象的 Window。但是从你满是管道的问题来看,我假设你使用的是基于 Unix 的操作系统 :)

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

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>]

回答by S.Lott

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

坏消息。从 Unix 命令行的角度来看,您的程序的这两次调用相同的。

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.

Unix 不能轻易区分它们。您所要求的并不是真正明智的,您需要考虑使用程序的另一种方式。

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.

它应该启动GUI吗?如果是这样,您可能需要一个“-i”(--interactive)选项来指示您想要一个 GUI,而不是读取标准输入。

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

有时,您可以将管道与控制台区分开来,因为控制台设备是“/dev/tty”,但这不是可移植的。

回答by Zan Lynx

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.

我对 Python 命令一无所知,但您应该能够使用 poll 或 select 来查找准备在标准输入上读取的数据。

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

这可能是特定于 Unix 操作系统的,而在 Windows Python 上则有所不同。