標準輸入輸入我用兩個過程之間subprocess
交換數據子不能從其他過程
我編輯repeat.py
文件有:
此文件是從http://www.doughellmann.com/PyMOTW/subprocess/
import sys
sys.stderr.write('repeater.py: starting\n')
sys.stderr.flush()
while True:
next_line = sys.stdin.readline()
if not next_line:
break
sys.stdout.write(next_line)
sys.stdout.flush()
sys.stderr.write('repeater.py: exiting\n')
sys.stderr.flush()
一個例子
並運行此文件ipython
In [1]: import subprocess
In [2]: f=subprocess.Popen(['python','~/repeat.py'],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
In [3]: f.stdin.write('teststs\n')
In [4]: f.communicate()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'teststs' is not defined
Out[4]: ('', None)
爲什麼teststs
未定義?
@Sven Marnach感謝您的評論,它運行正常後,我刪除'殼=真' – timger 2012-07-19 14:53:10
好 - 我把評論變成了答案。 – 2012-07-19 14:54:21
相關:[不要在子進程中使用list參數和shell = True](http://bugs.python.org/issue21347) – jfs 2014-09-21 16:04:34