查阅子流程手册。你有选择
subprocess,以便能够重定向
stdin,
stdout以及
stderr过程中你打电话给你自己。
from subprocess import Popen, PIPE, STDOUTp = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)grep_stdout = p.communicate(input='onentwonthreenfournfivensixn')[0]print grep_stdout
您还可以逐行与过程交互。给定为
prog.py:
import sysprint 'what is your name?'sys.stdout.flush()name = raw_input()print 'your name is ' + namesys.stdout.flush()
您可以通过以下方式逐行与其交互:
>>> from subprocess import Popen, PIPE, STDOUT>>> p = Popen(['python', 'prog.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)>>> p.stdout.readline().rstrip()'what is your name'>>> p.communicate('mike')[0].rstrip()'your name is mike'
编辑:在python3中,它必须为
'mike'.enpre()。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)