我有以下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)