无法运行Popen.communicate()两次。 (Python子进程模块)

时间:2013-03-10 05:45:46

标签: python subprocess popen

如果我运行两次通信功能,有人可以解释为什么我会收到此错误吗?

例如

from subprocess import *
SVN=Popen('which svn', shell=True, stdout=PIPE)
print SVN.communicate()[0]

返回

"/usr/bin/svn"

但再次进行沟通......

print SVN.communicate()[0]

...返回

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",     line 746, in communicate
stdout = _eintr_retry_call(self.stdout.read)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 478, in _eintr_retry_call
return func(*args)
ValueError: I/O operation on closed file

1 个答案:

答案 0 :(得分:5)

因为“file”(实际上是被调用程序的stdout)已经关闭。这意味着您已经阅读了上一个communicate()中的所有输出,因此再次调用它永远不会产生任何结果。