capturing the output of external commands

Fernando Perez fperez528 at yahoo.com
Sun Jul 25 23:16:41 EDT 2004


Avi Kak wrote:

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

Yes.

> 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/commands.py
Definition:     commands.getoutput(cmd)
Docstring:
    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.

Best,

f



More information about the Python-list mailing list