Piping popen stderr and stdout

Confer http://docs.python.org/library/subprocess.html.

communicate() returns a tuple (stdoutdata, stderrdata).

After the subprocess has finished, you can get the return code from the Popen instance:

Popen.returncode: The child return code, set by poll() and wait() (and indirectly by communicate()).

Likewise, you can achieve your goals like that:

sp = subprocess.Popen([executable, arg1, arg2], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
if out:
    print "standard output of subprocess:"
    print out
if err:
    print "standard error of subprocess:"
    print err
print "returncode of subprocess:"
print sp.returncode

By the way, I would change the test

    if script.endswith('.*~') or script == 'README':
         continue

into a positive one:

if not filename.endswith(".sh"):
    continue

It is better to be explicit about what you would like to execute than being explicit about what you do not want to execute.

Also, you should name your variables in a more general fashion, so script should be filename in the first place. As listdir also lists directories, you can explicitly check for those. Your current try/except block is not proper as long as you do not handle a specific exception. Instead of abspath, you should just concatenate initdir and filename, which is a concept often applied in context of os.listdir(). For security reasons, use shell=True in the constructor of the Popen object only if you are absolutely sure that you require it. Let me suggest the following:

for filename in sorted(os.listdir(initdir), reverse=reverse):
    if os.path.isdir(filename) or not filename.endswith(".sh"):
         continue
    if os.access(script, os.X_OK):
        exepath = os.path.join(initdir, filename)
        sp = subprocess.Popen(
            (exepath, 'stop' if reverse else 'start'),
            stderr=subprocess.PIPE,
            stdout=subprocess.PIPE)
        out, err = sp.communicate()
        print out, err, sp.returncode

Leave a Comment