469,909 Members | 1,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

capturing the output of external commands



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.

The function os.system() or any of the
os.exec functions do not work for what I
have in mind because they do not capture
and return in Python the output produced
by the commands supplied to them as
arguments.

Avi Kak
ka*@purdue.edu

Jul 18 '05 #1
4 2257
See the standard 'commands' module. If that doesn't fit your needs, you
can capture the output directly with os.popen.

On Sun, Jul 25, 2004 at 02:51:06PM +0000, 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.

The function os.system() or any of the
os.exec functions do not work for what I
have in mind because they do not capture
and return in Python the output produced
by the commands supplied to them as
arguments.

Avi Kak
ka*@purdue.edu

Jul 18 '05 #2


Thanks, Phil. The function commands.getoutput()
is exactly what I was looking for.

Avi


On Sun, 25 Jul 2004 11:09:47 -0400, Phil Frost <in****@bitglue.com>
wrote:
See the standard 'commands' module. If that doesn't fit your needs, you
can capture the output directly with os.popen.

On Sun, Jul 25, 2004 at 02:51:06PM +0000, 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.

The function os.system() or any of the
os.exec functions do not work for what I
have in mind because they do not capture
and return in Python the output produced
by the commands supplied to them as
arguments.

Avi Kak
ka*@purdue.edu


Jul 18 '05 #3
Avi Kak <ka*@purdue.edu> wrote in message news:<0p********************************@4ax.com>. ..
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.


You can fake it by redirecting the output of whatever external command
you want to run to a file, and then reading the file into a string.
It seems like kind of a hack (how does Perl's implementation actually
work for this, though?), but it will work. Package it up as a
function and you're set.
Jul 18 '05 #4
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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Fernando Rodriguez | last post: by
14 posts views Thread by jas | last post: by
2 posts views Thread by Doru-Catalin Togea | last post: by
1 post views Thread by Marcel | last post: by
5 posts views Thread by amit.uttam | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.