Subprocess with and without shell
Nick Craig-Wood
nick at craig-wood.com
Tue May 15 05:30:03 EDT 2007
George Sakkis <george.sakkis at gmail.com> wrote:
> I'm trying to figure out why Popen captures the stderr of a specific
> command when it runs through the shell but not without it. IOW:
>
> cmd = [my_exe, arg1, arg2, ..., argN]
> if 1: # this captures both stdout and stderr as expected
> pipe = Popen(' '.join(cmd), shell=True, stderr=PIPE, stdout=PIPE)
> else: # this captures only stdout
> pipe = Popen(cmd, shell=False, stderr=PIPE, stdout=PIPE)
>
> # this prints the empty string if not run through the shell
> print "stderr:", pipe.stderr.read()
> # this prints correctly in both cases
> print "stdout:", pipe.stdout.read()
>
> Any hints ?
Post an example which replicates the problem!
My effort works as expected
-- z.py ----------------------------------------------------
#!/usr/bin/python
from subprocess import Popen, PIPE
cmd = ["./zz.py"]
for i in range(2):
if i: # this captures both stdout and stderr as expected
print "With shell"
pipe = Popen(' '.join(cmd), shell=True, stderr=PIPE, stdout=PIPE)
else: # this captures only stdout
print "Without shell"
pipe = Popen(cmd, shell=False, stderr=PIPE, stdout=PIPE)
# this prints the empty string if not run through the shell
print "stderr:", pipe.stderr.read()
# this prints correctly in both cases
print "stdout:", pipe.stdout.read()
---zz.py----------------------------------------------------
#!/usr/bin/python
import sys
print >>sys.stdout, "Stdout"
print >>sys.stderr, "Stderr"
------------------------------------------------------------
Produces
$ ./z.py
Without shell
stderr: Stderr
stdout: Stdout
With shell
stderr: Stderr
stdout: Stdout
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list