pipe = subprocess.Popen(['clisp'],stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(response,err) = pipe.communicate("(+ 1 1)\n(* 2 2)")
#only print the last 6 lines to chop off the REPL intro text.
#Obviously you can do whatever manipulations you feel are necessary
#to correctly grab the input here
print '\n'.join(response.split('\n')[-6:])
与另一个进程通信的示例:
注意,communicate将在它运行后关闭流,因此您必须提前知道所有命令,此方法才能工作。好像管道.stdout在stdin关闭前不冲水?我很好奇是否有一条路是我错过的。你知道吗
您希望捕获另一个命令的输出。为此使用^{} module 。你知道吗
如果命令需要交互式输入,则有两个选项:
使用
subprocess.Popen()
对象,将stdin
参数设置为subprocess.PIPE
,并将输入写入可用的stdin管道。对于一个输入参数,这就足够了。详细研究subprocess
模块的文档,但基本的交互是:使用^{} library 驱动进程。这样,您就可以通过查找模式来创建与子流程更复杂的交互,即子流程生成的输出:
你可以试试:
相关问题 更多 >
编程相关推荐