Windows : Why does this not work? 上的 Python 管道

标签 python windows piping

我正在尝试这样的事情

输出.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 重定向,您可以将此值添加到注册表(在我的机器上测试并验证它按需要工作)。

  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.

关于Windows : Why does this not work? 上的 Python 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/466801/

相关文章:

python - 如何在终端中从 python 设置可选参数?

c++ - 不显示由 CreateFile() 函数创建的文本文件

java - 在 Java 中通过管道将 Unix 命令组合在一起

python + matplotlib : how to make a bar diagram combining 1 bar and 2 bars?

python - 导入错误 : no module named flask. ext.script

python - 在python中用逗号作为小数点分隔符编写csv

windows - 如何在批处理中使用 netstat 命令后打开 .txt 文件

android - 在 Android SDK 中设置 JAVA_HOME

c - 在不创建特定管道描述符的情况下进行管道传输

Linux管道和循环