【发布时间】:2012-05-27 05:47:31
【问题描述】:
如何实现以下功能:
- Python执行一个shell命令,等待用户
input某事 - 在用户输入
input后,程序会回复一些output - Python 捕获
output
【问题讨论】:
-
在更一般的情况下,pexpect 可能有用。
如何实现以下功能:
input某事input后,程序会回复一些output
output
【问题讨论】:
你可能想要subprocess.Popen。要与进程通信,您可以使用communicate 方法。
例如
process=subprocess.Popen(['command','--option','foo'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
inputdata="This is the string I will send to the process"
stdoutdata,stderrdata=process.communicate(input=inputdata)
【讨论】: