2013-03-10 64 views
3

任何人都可以解释为什么我得到这个错误,如果我运行通信功能两次?无法运行两次Popen.communicate()。 (Python子进程模块)

例如

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 

回答

5

因为实际上是正在调用的程序的stdout的“文件”已关闭。这意味着您已经读取了之前communicate()中的所有输出,因此再次调用永远不会产生任何结果。

相关问题