Python 执行命令行,发送输入和读取输出 [英] Python execute command line,sending input and reading output
本文介绍了Python 执行命令行,发送输入和读取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何实现以下功能:
- Python 执行一个 shell 命令,它等待用户
input
一些东西 - 在用户输入
input
后,程序响应一些output
- Python 捕获
输出
- Python executes a shell command, which waits for the user to
input
something - after the user typed the
input
, the program responses with someoutput
- Python captures the
output
推荐答案
您可能需要 subprocess.Popen
.要与进程通信,您可以使用 communicate
方法.
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屋!
查看全文