Best option to launch application and cath stderror and stdoutput

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...

Who is Participating?
RichieHindleConnect With a Mentor Commented:
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:
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.
Did that answer your question?  Is there any more information you need?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.