Best option to launch application and cath stderror and stdoutput

Posted on 2005-05-13
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...

Question by:RobertoDeAlba
    LVL 14

    Accepted Solution

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

    Expert Comment

    Did that answer your question?  Is there any more information you need?

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" " You can also get information on Python scripting language from the above mentioned we…
    Article by: Swadhin
    Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
    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 …

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now