Windows上的Python管道:为什么这行不通? [英] Python piping on Windows: Why does this not work?

查看:142
本文介绍了Windows上的Python管道:为什么这行不通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试类似的事情

Output.py

Output.py

print "Hello"

Input.py

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

在cmd行

Output.py | Input.py

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

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

感谢您的帮助.

编辑
帕特里克·哈灵顿解决方案可行,但我没有不知道为什么...

EDIT
Patrick Harrington solution works but I don't know why...

推荐答案

我在Windows计算机上对此进行了测试,如果您指定Python exe,它也可以使用:

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

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

但是如果直接以以下方式运行命令,也会出现EOFError:

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

output.py | input.py 

我不确定为什么会这样,我仍在调查这一问题,但至少现在应该为您提供一种解决方法.它可能与为.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.

更新:恩,你知道什么.看起来这实际上是Windows中的错误,其中从文件关联启动stdin/stdout重定向可能无法正常工作.因此,解决方法正如我和Patrick所指出的那样,您需要指定"python"将运行input.py,否则它将不会将stdout从output.py正确重定向到stdin,以用于input.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.

参考:

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

更新2 :

要更改此行为并使Windows管道按预期的方式进行stdin/stdout重定向,可以将此值添加到注册表中(在我的盒子上经过测试,并根据需要验证了此功能).

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

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

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

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

在编辑"菜单上,单击添加值",然后添加以下内容 注册表值:

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

值名称: InheritConsoleHandles
数据类型: REG_DWORD
基数:小数
值数据:1

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

退出注册表编辑器.

这篇关于Windows上的Python管道:为什么这行不通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆