从python中的stdin读取时管道文件描述错误

sha*_*kin 2 python windows pipe

这个问题重复一遍.投票结束.

在Windows命令行考虑这个.

scriptA.py | scriptB.py
Run Code Online (Sandbox Code Playgroud)

在scriptA.py中:

sys.stdout.write( "hello" )
Run Code Online (Sandbox Code Playgroud)

在scriptB.py中:

print sys.stdin.read()
Run Code Online (Sandbox Code Playgroud)

这会生成以下错误:

c:\> scriptA.py | scriptB.py
close failed: [Errno 22] Invalid argument
Traceback (most recent call last):
  File "c:\scriptB.py", line 20, in <module>
    print sys.stdin.read()
IOError: [Errno 9] Bad file descriptor
Run Code Online (Sandbox Code Playgroud)

"关闭失败"消息似乎来自scriptA.py的执行.

如果我使用sys.stdin.read(),sys.stdin.read(1),sys.stdin.readlines()等等并不重要.

怎么了?

这个问题重复一遍.投票结束.

Joh*_*han 7

从文件关联开始,似乎stdin/stdout重定向不起作用.这不是特定于python,而是由win32 cmd.exe引起的问题.

请参阅:http://mail.python.org/pipermail/python-bugs-list/2004-August/024920.html