Getting subprocess.call() output into a string?

Tobiah toby at tobiah.org
Tue Apr 15 22:36:11 CEST 2008


I am not sure how to capture the output of a command
using subprocess without creating a temp file.  I was
trying this:

import StringIO
import subprocess

file = StringIO.StringIO()

subprocess.call("ls", stdout = file)

Traceback (most recent call last):
  File "<stdin>", line 6, in ?
  File "/usr/local/lib/python2.4/subprocess.py", line 413, in call
    return Popen(*args, **kwargs).wait()
  File "/usr/local/lib/python2.4/subprocess.py", line 534, in __init__
    (p2cread, p2cwrite,
  File "/usr/local/lib/python2.4/subprocess.py", line 840, in _get_handles
    c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'

So how do I get the output into a string?

I thought that the idea of StringIO was that it could be
used where a file was expected.

Thanks,

Toby
** Posted from http://www.teranews.com **



More information about the Python-list mailing list