我正在尝试这样的事情
输出.py
print "Hello"
输入.py
greeting = raw_input("Give me the greeting. ")
print "The greeting is:", greeting
在命令行
Output.py | Input.py
但它返回一个 EOFError。谁能告诉我我做错了什么?
感谢您的帮助。
编辑
帕特里克·哈灵顿 solution有效,但我不知道为什么...
最佳答案
我在我的 Windows 机器上测试了这个,如果你指定 Python exe,它就可以工作:
C:\>C:\Python25\python.exe output.py | C:\Python25\python.exe input.py
Give me the greeting. The greeting is: hello
但如果直接运行命令,我也会得到一个 EOFError:
output.py | input.py
我不确定这是为什么,我仍在研究这个问题,但至少这应该为您提供一个解决方法。它可能与为 .py 文件调用文件处理程序的方式有关。
更新:嗯,你知道什么。看起来这实际上是 Windows 中的一个错误,当从文件关联启动时,stdin/stdout 重定向可能无法正常工作。因此,解决方法如我自己和帕特里克所指出的那样,您需要指定“python”将运行 input.py,否则它不会正确地将 stdout 从 output.py 重定向到 input.py 的 stdin。
引用:
http://mail.python.org/pipermail/python-bugs-list/2004-August/024923.html
http://support.microsoft.com/default.aspx?kbid=321788
更新 2:
要更改此行为并使 Windows 管道按预期工作以进行 stdin/stdout 重定向,您可以将此值添加到注册表(在我的机器上测试并验证它按需要工作)。
- Start Registry Editor.
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:
Value name: InheritConsoleHandles
Data type: REG_DWORD
Radix: Decimal
Value data: 1Quit Registry Editor.
关于Windows : Why does this not work? 上的 Python 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/466801/