如何从命令行将 EOF 发送到 Python sys.stdin?CTRL-D 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1892215/
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 23:17:47  来源:igfitidea点击:

How to send EOF to Python sys.stdin from commandline? CTRL-D doesn't work

pythonunixcommand-linebuffer

提问by Joseph Turian

I am writing to my Python process from the commandline on unix. I want to send EOF (or somehow flush the stdin buffer, so Python can read my input.)

我正在从 unix 上的命令行写入我的 Python 进程。我想发送 EOF(或以某种方式刷新 stdin 缓冲区,以便 Python 可以读取我的输入。)

If I hit CTRL-C, I get a KeyboardError.

如果我按 CTRL-C,我会收到一个键盘错误。

If I hit CTRL-D, the program just stops.

如果我按下 CTRL-D,程序就会停止。

How do I flush the stdin buffer?

如何刷新标准输入缓冲区?

采纳答案by Joseph Turian

If you use 'for l in sys.stdin', it is buffered.

如果您在 sys.stdin 中使用“for l”,它会被缓冲。

You can use:

您可以使用:

  while 1:
     l = sys.stdin.readline()

回答by Alex Martelli

Control-D should NOT make your program "just stop" -- it should close standard input, and if your program deals with that properly, it may perfectly well continue if it needs to!

Control-D 不应该让你的程序“停止”——它应该关闭标准输入,如果你的程序处理得当,它可能会在需要时继续运行!

For example, given the following st.py:

例如,给定以下内容st.py

import sys

def main():
  inwas = []
  for line in sys.stdin:
    inwas.append(line)
  print "%d lines" % len(inwas),
  print "initials:", ''.join(x[0] for x in inwas)

if __name__ == '__main__':
  main()

we could see something like

我们可以看到类似的东西

$ python st.py
nel mezzo del cammin di nostra vita
mi ritrovai per una selva oscura
che la diritta via era smarrita
3 lines initials: nmc
$ 

if the control-D is hit right after the enter on the third line -- the program realizes that standard input is done, and performs the needed post-processing, all neat and proper.

如果在第三行输入后立即按下 control-D - 程序意识到标准输入已完成,并执行所需的后处理,一切都井井有条。

If your program prematurely exits on control-D, it must be badly coded -- what about editing you question to add the smallest "misbehaving" program you can conceive of, so we can show you exactly HOW you're going wrong?

如果您的程序在 control-D 上过早退出,那么它一定是编码错误——编辑您的问题以添加您能想到的最小的“行为不端”程序怎么样,这样我们就可以确切地向您展示您是如何出错的?

回答by Alok Singhal

I think I know what's happening. You are hitting ctrl-Dwithout hitting enter. If you want to send a line to the program, just hit enter. If you hit ctrl-Dwithout hitting enter, you can hit ctrl-Dagain and your program should see the line then. In this case (two ctrl-Ds in succession), your program will not see a newline at the end of the line.

我想我知道发生了什么。你ctrl-D打不打enter。如果您想向程序发送一行,只需按 Enter。如果你ctrl-D没有击中enter,你可以ctrl-D再次击中,然后你的程序应该看到该行。在这种情况下(ctrl-D连续两个s),您的程序将不会在行尾看到换行符。

For example, let's say I have a Python script a.py:

例如,假设我有一个 Python 脚本a.py

import sys

for line in sys.stdin:
    sys.stdout.write('%s' % line)

And I execute it:

我执行它:

$ python a.py

And then enter the following:

然后输入以下内容:

line 1
line 2<ctrl-D><ctrl-D>

the program will print:

该程序将打印:

line 1
line 2$

$is the shell-prompt. Here's a full session with the above input:

$是 shell 提示。这是具有上述输入的完整会话:

$ python a.py
line 1
line 2 line1
line 2$

$ python a.py
line 1
line 2 line1
line 2$

(Boldshow the program's output. Roman-case is for showing what I typed, sans the two ctrl-Ds)

粗体显示程序的输出。罗马大小写用于显示我输入的内容,没有两个ctrl-Ds)

If this is not what's happening, you need to tell us more about what you are doing.

如果这不是正在发生的事情,您需要告诉我们更多有关您在做什么的信息。

回答by RdB

try:
    # You might be inside the while-loop
    foo = raw_input('Spam: ') # Or whatever...
except EOFError:
    print 'And now for something completely different.'
    sys.exit()