?
Solved

Best option to launch application and cath stderror and stdoutput

Posted on 2005-05-13
4
Medium Priority
?
171 Views
Last Modified: 2010-04-16
Hi  Python Experts

I have written a script that launch several commands with several options, this is for testing an application

I am using something like:

tochild, fromchild, childerror = os.popen3(my_command,'r')
err = childerror.readline()
output = fromchild.readline()

Now, I would like to asure that before err = childerror.readline() , all the process has efectively finished....

I know there is a a method call poll() and some other wait(), but I don know how to use them....
Or maybe there is a better way to do this...



0
Comment
Question by:RobertoDeAlba
  • 2
2 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 2000 total points
ID: 14014776
fromchild.readline() will block until either a line is written by the child, or the child exits.  So you can keep reading lines until you get an empty line, then you know it's finished.  Something like this (which I tested on Windows):

import os
my_command = "dir /s"
tochild, fromchild, childerror = os.popen3(my_command, 't')
outputLines = []
while 1:
    line = fromchild.readline()
    if line:
        outputLines.append(line)
    else:
        break
   
errorLines = childerror.readlines()

print "Output:"
print ''.join(outputLines)

print "Errors:"
print ''.join(errorLines)

This assumes that the child will never write so many error lines that it will block waiting for your script to read some of them.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 15340793
Did that answer your question?  Is there any more information you need?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question