Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping popen stderr and stdout

Tags:

python

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 script.endswith('.*~') or script == 'README':              continue         if os.access(script, os.X_OK):             try:                 execute = os.path.abspath(script)                 sp.Popen((execute, 'stop' if reverse else 'start'),                          stdin=None, stderr=sp.PIPE,                          stdout=sp.stderr, shell=True).communicate()             except:                 raise 

Now what i Want is: lets say i have a bash script with a start functiont. from which I call

echo "Something"

Now I want to see that echo on sys.stdout and the exit Code. I believe you do this with .communicate() but mine doesn't work the way i thought it would.

What am I doing wrong?

Any help is much appreciated

like image 695
wagner-felix Avatar asked May 21 '12 10:05

wagner-felix


People also ask

What is PIPE in Popen?

The popen() function executes the command specified by the string command. It creates a pipe between the calling program and the executed command, and returns a pointer to a stream that can be used to either read from or write to the pipe.

What is Popen and PIPE in Python?

Popen() takes two named arguments, one is stdin and the second is stdout. Both of these arguments are optional. These arguments are used to set the PIPE, which the child process uses as its stdin and stdout. The subprocess. PIPE is passed as a constant so that either of the subprocess.

What is Popen in subprocess?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What is Popen in Python?

Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.


1 Answers

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 
like image 71
Dr. Jan-Philip Gehrcke Avatar answered Oct 02 '22 17:10

Dr. Jan-Philip Gehrcke