简体   繁体   English

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

[英]Python piping on Windows: Why does this not work?

I'm trying something like this 我正在尝试这样的事情

Output.py Output.py

print "Hello"

Input.py 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 . 但它返回一个EOFError Can someone tell me what I am doing wrong? 有人能告诉我我做错了什么吗?

Thanks for your help. 谢谢你的帮助。

EDIT 编辑
Patrick Harrington solution works but I don't know why... Patrick Harrington 解决方案有效,但我不知道为什么......

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. 看起来这实际上是Windows中的一个错误,当从文件关联启动时,stdin / stdout重定向可能无法正常工作。 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. 所以解决方法是我自己和Patrick所指出的,你需要指定“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 HKEY_LOCAL_MACHINE \\ SOFTWARE \\微软\\的Windows \\ CurrentVersion \\政策\\ Explorer中

  3. On the Edit menu, click Add Value, and then add the following registry value: 在“编辑”菜单上,单击“添加值”,然后添加以下注册表值:

    Value name: InheritConsoleHandles 值名称: InheritConsoleHandles
    Data type: REG_DWORD 数据类型: REG_DWORD
    Radix: Decimal 基数:十进制
    Value data: 1 价值数据:1

  4. Quit Registry Editor. 退出注册表编辑器。

Change it to: 将其更改为:

Output.py | python Input.py

The output will be: 输出将是:

Give me the greeting. 给我问候。 The greeting is: hello 问候语是:你好

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM