Subprocess.stdout behaves differently with shell = True and shell = False

I have a python script called class.py with one line:

class.py:

print "Hello World"

I run it in a subprocess, for example.

run_subprocess.py:

import subprocess

p = subprocess.Popen(["python", "class.py"], stdin=subprocess.PIPE, shell=True,
                     stdout=subprocess.PIPE)
a = p.communicate()

print a

When I run this (on Mac), I get an empty command line:

$ python run_subprocess.py
('', None)

When I run this with shell = False, I get the contents of stdout:

run_subprocess.py:

import subprocess

p = subprocess.Popen(["python", "class.py"], stdin=subprocess.PIPE, shell=False,
                     stdout=subprocess.PIPE)
a = p.communicate()

print a


$ python run_subprocess.py
('hello world\n', None)

Why? How can I get the contents stdoutwhen starting a subprocess with shell=True?

Thanks Kevin

+5
source share
1 answer

Popen - shell. -. , ( ).

+5

All Articles