Skip to content Skip to sidebar Skip to footer

Piping Popen Stderr And Stdout

I want to call scripts from a directory (they are executable shell scripts) via python. so far so good: for script in sorted(os.listdir(initdir), reverse=reverse): if

Solution 1:

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('.*~') orscript== '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):
    ifos.path.isdir(filename) ornot filename.endswith(".sh"):
         continue
    ifos.access(script, os.X_OK):
        exepath = os.path.join(initdir, filename)
        sp = subprocess.Popen(
            (exepath, 'stop'ifreverseelse'start'),
            stderr=subprocess.PIPE,
            stdout=subprocess.PIPE)
        out, err = sp.communicate()
        print out, err, sp.returncode

Post a Comment for "Piping Popen Stderr And Stdout"