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'
Output from
subprocess.call()should only be redirected to files.You should use
subprocess.Popen()instead. Then you can passsubprocess.PIPEfor the stderr, stdout, and/or stdin parameters and read from the pipes by using thecommunicate()method:The reasoning is that the file-like object used by
subprocess.call()must have a real file descriptor, and thus implement thefileno()method. Just using any file-like object won’t do the trick.See here for more info.