且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免使用 python 处理空的标准输入?

更新时间:2023-11-17 23:52:34

grep 可以按照它的方式工作,因为它有一个非可选参数:模式.例如

grep can work the way it does because it has one non-optional argument: the pattern. For example

$ grep < /dev/zero
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

即使 stdin 上有无限的输入可用,grep 也没有得到所需的参数,因此抱怨.

even though there was infinite input available on stdin, grep didn't get the required argument and therefore complained.

如果您只想使用可选参数并在 stdin 是终端时出错,请查看 file.isatty().

If you want to use only optional arguments and error out if stdin is a terminal, look at file.isatty().