473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 2424
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fernando Rodriguez | last post by:
Hi, How can I save the output of an external program, called with os.system()?
2
by: PWR | last post by:
Platform: Windows Python: 2.3 Python Skill: Reading, Learning, but still newbie. Mission: My mission is writing a script that is triggered from my mail server. The script will execute and...
2
by: Hoegje | last post by:
I am writing a C++ program, which should create a sub- process to start a telnet session to another server. Then it should login to that server (on the telnet login) and execute one or more...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
14
by: jas | last post by:
I would like to redirect the output from os.system to a variable, but am having trouble. I tried using os.popen(..).read() ...but that doesn't give me exactly what i want. ...this is windows by...
2
by: Doru-Catalin Togea | last post by:
Hi! On solaris, I call a program from python with os.system("some command > outputFile.txt" ) This redirects the output of "some command"'s stdout to "outputFile.txt". However I need to...
1
by: Marcel | last post by:
Hi, I used the following code Jochen Kalmbach suggested to run an external program: System::Diagnostics::ProcessStartInfo *si = new System::Diagnostics::ProcessStartInfo();...
5
by: Luigi | last post by:
Hi to all! I'd like to execute an external program capturing the stdout/stderr messages at "real-time". I mean that I don't want to wait for the end of the process. If I write a code like this:...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.