How can I get the output of a process run using subprocess.call()?
Passing a StringIO.StringIO object to stdout gives this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles
c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>>
StringIOworks like a file in most cases but not all. It doesn't work in your case because themultiprocessingmodule assumes actual files in some cases. This may have been fixed: see bugs.python.org/issue5313 for a related bug. - Michael Greenecommunicate()usesselect.select(), which only accepts file descriptors, so it isn't really a bug. I was quite confused by this when I first encountered it and exploring the depths of subprocess.py taught me a lot!. - Mikesubprocess.runmakes this simpler, as of Python 3.5. I'll add an answer when I get a chance. - Mark Amerysubprocess.check_output(); in Python 3.5+ you will also want to look atsubprocess.run(). There should be no need or want to use rawsubprocess.Popen()if you can avoid it, though some more complex use cases require it (and then you have to do the required plumbing around it yourself). The Stack Overflowsubprocesstag info page has some good resources for the less trivial cases. - tripleee