Python执行命令行,发送输入和读取输出 [英] Python execute command line,sending input and reading output

查看:651
本文介绍了Python执行命令行,发送输入和读取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现以下功能:


  1. Python执行shell命令,等待用户输入某事

  2. 在用户输入输入后,程序会回复一些输出

  3. Python捕获输出

  1. Python executes a shell command, which waits for the user to input something
  2. after the user typed the input, the program responses with some output
  3. Python captures the output


推荐答案

你可能想要 subprocess.Popen 。要与流程进行通信,您可以使用沟通方法。

You probably want subprocess.Popen. To communicate with the process, you'd use the communicate method.

例如

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)

这篇关于Python执行命令行,发送输入和读取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆