On Tue, Apr 15, 2008 at 10:36 PM, Tobiah <[EMAIL PROTECTED]> wrote: > 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. >
For basic file-like read and write. But it won't provide a file handle since there is no 'real' file. Also, from 2.3.9 File Objects: "File-like objects which do not have a real file descriptor should not provide this method!" You should use the PIPE subprocess argument to capture output. From the tutorial: 6.8.3.1 Replacing /bin/sh shell backquote output=`mycmd myarg` ==> output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] -- http://mail.python.org/mailman/listinfo/python-list