473,395 Members | 1,556 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,395 software developers and data experts.

obtain the output of an external program

Hi,

How can I save the output of an external program, called with os.system()?
Jul 18 '05 #1
4 3521
Fernando Rodriguez wrote:
How can I save the output of an external program, called with os.system()?


You should use os.popen instead of os.system. It returns a file-like
object which you can read from (by default, e.g. when you call os.popen
with a single argument) and what you read is exactly the external
program's output.
Alex

Jul 18 '05 #2
Fernando Rodriguez <fr*@easyjob.net> writes:
How can I save the output of an external program, called with os.system()?


Are you sure you need to use os.system?
Python Library Documentation: module commands

NAME
commands - Execute shell commands via os.popen() and return status, output.

FILE
/usr/lib/python2.2/commands.py

DESCRIPTION
Interface summary:

import commands

outtext = commands.getoutput(cmd)
(exitstatus, outtext) = commands.getstatusoutput(cmd)
outtext = commands.getstatus(file) # returns output of "ls -ld file"

A trailing newline is removed from the output string.

Encapsulates the basic operation:

pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
text = pipe.read()
sts = pipe.close()

[Note: it would be nice to add functions to interpret the exit status.]
--
# Edvard Majakari Software Engineer
# PGP PUBLIC KEY available Soli Deo Gloria!
One day, when he was naughty, Mr Bunnsy looked over the hedge into Farmer
Fred's field and it was full of fresh green lettuces. Mr Bunnsy, however, was
not full of lettuces. This did not seem fair. --Mr Bunnsy has an adventure
Jul 18 '05 #3
Fernando Rodriguez <fr*@easyjob.net> wrote in message news:<n0********************************@4ax.com>. ..
Hi,

How can I save the output of an external program, called with os.system()?


Use commands.getoutput() instead of os.system():

import commands
xy = commands.getoutput(external_program)

Regards

Michael
Jul 18 '05 #4
Hello Fernando,
How can I save the output of an external program, called with os.system()?

1. Redirect the output of the program to a file (using "> fname" in
the system command) and read it later. Be aware that stderr is not
captured this way.
2. Use os.popen and variants.

IMO option 2 is better.

HTH.
Miki
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hugh Macdonald | last post by:
I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it) I can do one or other...
3
by: Yuan HOng | last post by:
In my program I have to call an external program and parse its output. For that I use the os.popen2 function, and then read the output stream. But the complexity is that the external program...
4
by: Richard Tierney | last post by:
To create help output (the response to "myprog --help", for example) I currently create a big .h file, which includes a single string, such as: static char *help_text = "\ myprog: my program\n\...
5
by: Kuku | last post by:
Would the following program give the same output at all times int main(int argc, char *argv) { strcpy(argv, "Hello"); strcpy(argv,"Good Morning"); printf("%s\n%s\n",argv,argv); return 0; }
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();...
14
by: fdu.xiaojf | last post by:
Hi, I'm writing a program which imports an external module writing in C and calls a function provided by the module to do my job. But the method produces a lot of output to the stdout, and this...
2
by: qwertycat | last post by:
Is it possible to execute programs in a PHP script without waiting for the output and successfully ending the PHP script without ending the external program too?
4
by: JohnMoulding | last post by:
Hi, I'm a newbie, so apologies for missing the obvious... and I have searched for an answer to this question, but all I get is "how to call an external program from a C# program" - which is not...
6
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.