Getting output into a string?

Tobiah toby at
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()"ls", stdout = file)

Traceback (most recent call last):
  File "<stdin>", line 6, in ?
  File "/usr/local/lib/python2.4/", line 413, in call
    return Popen(*args, **kwargs).wait()
  File "/usr/local/lib/python2.4/", line 534, in __init__
    (p2cread, p2cwrite,
  File "/usr/local/lib/python2.4/", 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.


** Posted from **

More information about the Python-list mailing list