5
我有一個名爲class.py有一行Python腳本:subprocess.stdout與殼= True和外殼不同的行爲=假
class.py:
print "Hello World"
我在子進程運行, 喜歡這個。
run_subprocess.py:
import subprocess
p = subprocess.Popen(["python", "class.py"], stdin=subprocess.PIPE, shell=True,
stdout=subprocess.PIPE)
a = p.communicate()
print a
當我運行這個(在Mac上),我得到一個空的命令字符串:
$ python run_subprocess.py
('', None)
當我運行這個與外殼=假,我得到標準輸出的內容:
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)
爲什麼?如何在shell=True
上運行子進程時獲得stdout
的內容?
謝謝, 凱文