Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run Popen.communicate() twice. (Python subprocess module)

Can anyone explain why I get this error if I run the communicate function twice?

For instance

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

returns

"/usr/bin/svn"

but running communicate again...

print SVN.communicate()[0]

returns...

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
like image 931
imagineerThat Avatar asked Sep 02 '25 05:09

imagineerThat


1 Answers

Because the "file", which is actually the stdout of the program being invoked, has been closed. This means you have already read all the output in the previous communicate(), so calling it again can never produce anything.

like image 78
John Zwinck Avatar answered Sep 04 '25 21:09

John Zwinck