2013-03-10 68 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()中的所有輸出,因此再次調用永遠不會產生任何結果。

相關問題