Piping popen stderr and stdout
Question:
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
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
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
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