相关疑难解决方法(0)

为什么我必须按Ctrl + D两次才能关闭标准输入?

我有以下Python脚本,如果输入不是数字,则读取数字并输出错误.

import fileinput
import sys
for line in (txt.strip() for txt in fileinput.input()):
    if not line.isdigit():
        sys.stderr.write("ERROR: not a number: %s\n" % line)
Run Code Online (Sandbox Code Playgroud)

如果我从stdin获得输入,我必须按Ctrl+ D 两次才能结束程序.为什么?

当我自己运行Python解释器时,我只需要按Ctrl+ D一次.

bash $ python test.py
1
2
foo
4
5
<Ctrl+D>
ERROR: not a number: foo
<Ctrl+D>
bash $
Run Code Online (Sandbox Code Playgroud)

python bash stdin

13
推荐指数
3
解决办法
5366
查看次数

标签 统计

bash ×1

python ×1

stdin ×1