Capturing output of os.system to a string

Stephen Hansen apt.shansen at gmail.com
Mon Nov 23 20:36:35 EST 2009


On Mon, Nov 23, 2009 at 5:27 PM, gerry.butler <gabutler at acslink.net.au>wrote:

> How do I capture output to a string? For example, the output of
> os.system('whoami').
>
> I guess I need to redirect stdout, but I'm a total beginner, and I
> haven't been able to find out from the tutorials how to do this.
>
>
You don't; os.system is only usable to send commands off that need no input
and which you don't care for the output. Check out the subprocess module.

You can do like:

popen = subprocess.Popen("whoami", stdout=subprocess.PIPE)

Then "out, err = popen.communicate()"

The 'out' should contain the output from the command.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091123/41e9eb2c/attachment-0001.html>


More information about the Python-list mailing list