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

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

问题描述

如何实现以下功能:

  1. Python 执行一个 shell 命令,它等待用户input 一些东西
  2. 在用户输入input后,程序响应一些output
  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.要与进程通信,您可以使用 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屋!

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