【问题标题】:Python execute command line,sending input and reading outputPython执行命令行,发送输入和读取输出
【发布时间】:2012-05-27 05:47:31
【问题描述】:

如何实现以下功能:

  1. Python执行一个shell命令,等待用户input某事
  2. 在用户输入input后,程序会回复一些output
  3. Python 捕获output

【问题讨论】:

标签: python io


【解决方案1】:

你可能想要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)

【讨论】:

  • 如果我有 2 个或更多输入,我想在后台运行它,这样我就看不到控制台了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多