correct way of running a sub-process

Thomas Guettler guettli at thomas-guettler.de
Thu Feb 12 11:17:48 EST 2004


Am Thu, 12 Feb 2004 06:10:01 -0800 schrieb Daniel Timothy Bentley:

> What is the (hopefully unique) obvious way of runnign a sub-process if I
> want to get the exit code and input without resorting to multi-threading?
> 
> It seems like I should be able to do the following:
> 
> foo = popen2.Popen3(cmd)
> foo.wait()
> foo.fromchild.read()

I do it like this:

def shell_command(cmd):
    # There mustnot be output to stdout or stderr
    # otherwise an exception is raised

    p=popen2.Popen4(cmd) # read stdout and stderr
    output=p.fromchild.read()
    ret=p.wait()
    if ret or output:
        raise("Error in shell_command '%s': ret=%s output='%s'" %(
            cmd, ret, output))





More information about the Python-list mailing list