Windows 上的 Python 管道:为什么这不起作用?

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

Python piping on Windows: Why does this not work?

pythonwindowspiping

提问by Nope

I'm trying something like this

我正在尝试这样的事情

Output.py

输出.py

print "Hello"

Input.py

输入文件

greeting = raw_input("Give me the greeting. ")
print "The greeting is:", greeting

At the cmd line

在 cmd 行

Output.py | Input.py

But it returns an EOFError. Can someone tell me what I am doing wrong?

但它返回一个EOFError。有人可以告诉我我做错了什么吗?

Thanks for your help.

谢谢你的帮助。

EDIT
Patrick Harrington solutionworks but I don't know why...

编辑
Patrick Harrington解决方案有效,但我不知道为什么......

回答by Jay

I tested this on my Windows machine and it works if you specify the Python exe:

我在我的 Windows 机器上测试了这个,如果你指定 Python exe,它就可以工作:

C:\>C:\Python25\python.exe output.py | C:\Python25\python.exe input.py
Give me the greeting. The greeting is: hello

But I get an EOFError also if running the commands directly as:

但是,如果直接将命令运行为以下命令,我也会收到 EOFError:

output.py | input.py 

I'm not sure exactly why that is, I'm still looking into this one but at least this should provide you with a workaround for now. It may have something to do with the way the file handler is invoked for .py files.

我不确定为什么会这样,我仍在研究这个问题,但至少现在应该为您提供解决方法。它可能与为 .py 文件调用文件处理程序的方式有关。

UPDATE: well, what do you know. Looks like this is actually a bug in Windows where stdin/stdout redirection may not work properly when started from a file association. So the workaround is as noted by myself and Patrick, you need to specify "python" will be running input.py, otherwise it will not redirect stdout from output.py to the stdin for input.py correctly.

更新:嗯,你知道什么。看起来这实际上是 Windows 中的一个错误,当从文件关联启动时,stdin/stdout 重定向可能无法正常工作。所以解决方法是我和帕特里克指出的,你需要指定“python”将运行 input.py,否则它不会正确地将 stdout 从 output.py 重定向到 input.py 的 stdin。

Reference:

参考

http://mail.python.org/pipermail/python-bugs-list/2004-August/024923.html

http://mail.python.org/pipermail/python-bugs-list/2004-August/024923.html

http://support.microsoft.com/default.aspx?kbid=321788

http://support.microsoft.com/default.aspx?kbid=321788

UPDATE 2:

更新 2

To change this behavior and make Windows pipes work as expected for stdin/stdout redirection, you can add this value to the registry (tested on my box and verified this works as desired).

要更改此行为并使 Windows 管道按预期进行 stdin/stdout 重定向,您可以将此值添加到注册表(在我的机器上测试并验证它按需要工作)。

  1. Start Registry Editor.
  2. Locate and then click the following key in the registry:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

  3. On the Edit menu, click Add Value, and then add the following registry value:

    Value name: InheritConsoleHandles
    Data type: REG_DWORD
    Radix: Decimal
    Value data: 1

  4. Quit Registry Editor.

  1. 启动注册表编辑器。
  2. 在注册表中找到并单击以下项:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

  3. 在编辑菜单上,单击添加值,然后添加以下注册表值:

    值名称:InheritConsoleHandles
    数据类型:REG_DWORD
    基数:十进制
    值数据:1

  4. 退出注册表编辑器。

回答by Patrick Harrington

Change it to:

将其更改为:

Output.py | python Input.py

The output will be:

输出将是:

Give me the greeting. The greeting is: hello

给我打个招呼吧。问候语是:你好

回答by monkut

Here's why you get the EOFError (from the documentation on raw_input):

这就是您收到 EOFError 的原因(来自 raw_input 的文档):

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

然后该函数从输入中读取一行,将其转换为字符串(去除尾随的换行符),然后返回该字符串。读取 EOF 时,会引发 EOFError。

http://docs.python.org/library/functions.html?highlight=raw_input#raw_input

http://docs.python.org/library/functions.html?highlight=raw_input#raw_input

You may want to use sys.stdin, it provides a file object from which you can use the read, readlines methods.

您可能想使用sys.stdin,它提供了一个文件对象,您可以从中使用 read、readlines 方法。

import sys
for greeting_line in sys.stdin.readlines():
    print "The greeting is:", greeting_line.strip()