473,574 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get the stdout of the script that os.system executes

5 New Member
i know that in python, os.system(cmd) can be used to execute a script or command, and the return value is 0 if successful, rather than the standard output of the script that it executes.

e.g.

>>> a=os.system("ec ho hello")
>>> a
0 (rather than "hello")

my question is how to direct the standard output into a variable in my python script.
Oct 31 '07 #1
11 45164
KaezarRex
52 New Member
The only way I've been able to get output from os.system is to use ">" in the command to make the subshell write its output to a file. Then all I have to do is read the file.
Expand|Select|Wrap|Line Numbers
  1. >>> os.system("echo hello > output.txt")
  2.  
  3. >>> reader = open("output.txt")
  4. >>> print reader.read()
  5. hello 
If you want to just append to the file instead of truncating then appending, use ">>" instead of ">".

It would be nice if there was a more direct method, so hopefully someone else knows one. I'd be very interested to know what it is.
Oct 31 '07 #2
Smygis
126 New Member
It would be nice if there was a more direct method, so hopefully someone else knows one. I'd be very interested to know what it is.
Tere is the commands module.

[code=python]
>>> import commands
>>> print commands.getout put("echo helllo")
helllo
>>> import commands
>>> print commands.getout put("echo hello")
hello
>>> txt = commands.getout put("uname -a")
>>> print txt
Linux Bob 2.6.20-15-server #2 SMP Sun Apr 15 07:41:34 UTC 2007 i686 GNU/Linux
[code]

I also think os.popen works. but i have no experience with that. commands is quick and easy. i think os.popen is more advanced
Oct 31 '07 #3
bartonc
6,596 Recognized Expert Expert
i know that in python, os.system(cmd) can be used to execute a script or command, and the return value is 0 if successful, rather than the standard output of the script that it executes.

e.g.

>>> a=os.system("ec ho hello")
>>> a
0 (rather than "hello")

my question is how to direct the standard output into a variable in my python script.
Since you have not stated your OS, our freind Smygis has given a great solution (if you are running under *nix). I'd recommend, however, that you use the newer subprocess module.
6.8 subprocess -- Subprocess management

New in version 2.4.

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as:


os.system
os.spawn*
os.popen*
popen2.*
commands.*
Oct 31 '07 #4
bartonc
6,596 Recognized Expert Expert
Since you have not stated your OS, our freind Smygis has given a great solution (if you are running under *nix). I'd recommend, however, that you use the newer subprocess module.
Try:
Expand|Select|Wrap|Line Numbers
  1. import subprocess as sub
  2. retcode = sub.call(["ls", "-l"])
And let us know how that works out for you.
Oct 31 '07 #5
bartonc
6,596 Recognized Expert Expert
Try:
Expand|Select|Wrap|Line Numbers
  1. import subprocess as sub
  2. retcode = sub.call(["ls", "-l"])
And let us know how that works out for you.
On Windows, this:
Expand|Select|Wrap|Line Numbers
  1. >>> import subprocess as sub
  2. >>> sub.call(['dir'], shell=True)
worked in a Python command-line shell, but not in my IDE (Boa Constructor).
Oct 31 '07 #6
Smygis
126 New Member
Try:
Expand|Select|Wrap|Line Numbers
  1. import subprocess as sub
  2. retcode = sub.call(["ls", "-l"])
And let us know how that works out for you.
Still, that prints the result. And does not return it.

So no, Ill stick with commands.

Whenever i need something like this. And thats only happend like two times.
Oct 31 '07 #7
wxy212
5 New Member
Thanks for all of your replies.

"var = commands.getout put(cmd)" seems to be good. However, it doesn't check if the command has been executed successfully.

What i want is if it's executed successfully, then write the stdout to a variable, otherwise die with an error message..

If i use "if os.system(cmd) != 0:" to check it before running "var=commands.g etoutput(cmd)", then the stdout will not only be written to variable var, but also pop on the prompt screen, which is not what I like..

Does anyone have some idea?
Oct 31 '07 #8
Smygis
126 New Member
Thanks for all of your replies.

"var = commands.getout put(cmd)" seems to be good. However, it doesn't check if the command has been executed successfully.

What i want is if it's executed successfully, then write the stdout to a variable, otherwise die with an error message..

If i use "if os.system(cmd) != 0:" to check it before running "var=commands.g etoutput(cmd)", then the stdout will not only be written to variable var, but also pop on the prompt screen, which is not what I like..

Does anyone have some idea?
commands.getsta tusoutput

Expand|Select|Wrap|Line Numbers
  1. >>> fail = commands.getstatusoutput("ps -q")
  2. >>> notfail = commands.getstatusoutput("ps -A")
  3. >>> fail[0]
  4. 256
  5. >>> len(fail[1]) # the output
  6. 1517
  7. >>> notfail[0]
  8.  
  9. >>> len(notfail[1]) # the output
  10. 3408
  11.  
  12.  
Oct 31 '07 #9
wxy212
5 New Member
commands.getsta tusoutput

Expand|Select|Wrap|Line Numbers
  1. >>> fail = commands.getstatusoutput("ps -q")
  2. >>> notfail = commands.getstatusoutput("ps -A")
  3. >>> fail[0]
  4. 256
  5. >>> len(fail[1]) # the output
  6. 1517
  7. >>> notfail[0]
  8.  
  9. >>> len(notfail[1]) # the output
  10. 3408
  11.  
  12.  


brilliant!! thanks a lot..
Oct 31 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
3311
by: Sticks | last post by:
ok... im not quite sure how to describe my problem. i have a php script that runs through my entire php site and writes the resulting output to html files. this is necessary as the nature of the hosting available to me for this particular page prohibits me from using php/mysql as i would like. my script works simply by using output buffers...
1
1945
by: Ben Floyd | last post by:
It goes like this: Im converting a perl script that executes a command on each result of a directory listing. In perl, all is well. In Python, using approximately the same logic creates a seemingly random problem I can't pin down. I hesitated to ask this question since it's so Slackware Linux-centric, and im new to Python. It's hard to ask...
4
10036
by: Paul Nilsson | last post by:
Hi, Does anyone know how to redirect text output when issuing a system or an execlp call? I have a GUI that insists on opening up a console whenever I give a system function call, and I would like to redirect the text output to a char buffer instead. Paul
5
2453
by: alexLIGO | last post by:
Hi, I am trying to run a python script that executes several other programs on the bash (under linux) and reads some of the output of those programs. This works fine for a couple of os.system() calls, but then it does not work anymore. os.system() returns always -1, but when executing exactly the same program directly on the linux-bash it...
1
1454
by: Russ | last post by:
I've been trying to get my head around this for 3 days now and it seems like everything I try does not work for one reason or another. I built a test page using the TabStrip and MultiPage controls. The example shows how to put up pictures in each tab, and it works fine. But of course I don't want pictures, I want a webform where the data...
0
1566
by: Elad | last post by:
Hello All, I am trying to capture some printf's from a C function called by python. <Working under winxp> I have tried to following: STDOUT = 1 # stdout fd (re, we) = os.pipe() # Create re / write handlers
6
9910
by: tatamata | last post by:
Hello. How can I run some Python script within C# program? Thanks, Zlatko
8
3559
by: grikdog | last post by:
How to I capture stdout output from a system call and pipe it back into my running script? system "echo foobar"; # how to continue here with "foobar" in stdin ?? ... I can fake it with pipes and additional scripts, but that seems like overkill.
1
1560
by: vivek samantray | last post by:
i have a script which executes several queries for n number of records.How can is set the time in that to know how much time each query took to run
0
7807
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8058
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8241
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6456
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.