capturing the output of external commands

Fernando Perez fperez528 at
Mon Jul 26 05:16:41 CEST 2004

Avi Kak wrote:

> Is there a Python function in any of the
> standard-distribution modules that does
> what the backticks do in Perl?


> I want to run an external command and I'd
> like its output to be captured directly
> in my Python script in the form of a string
> object.

In [1]: import commands

In [2]: commands.getoutput?
Type:           function
Base Class:     <type 'function'>
String Form:    <function getoutput at 0x874a094>
Namespace:      Interactive
File:           /usr/lib/python2.2/
Definition:     commands.getoutput(cmd)
    Return output (stdout or stderr) of executing cmd in a shell.

You can get fancy using popen, and capture separately stdout/err, but for a
simple capture getoutput should do.



More information about the Python-list mailing list