python - 在 Python 脚本中包装交互式命令行应用程序

标签 python command-line

我有兴趣通过 Python 调用来控制交互式 CLI 应用程序。

我想在最基本的层面上,我需要一个 Python 脚本来在主机操作系统上启动 CLI 应用程序。从标准输入到 CLI 应用程序的任何内容,然后从 CLI 应用程序到标准输出的任何输出。

在此基础上,对输入和输出进行一些处理应该非常简单。

老实说,我可能只需要知道该技术的名称。我不知道我需要搜索什么。

最佳答案

也许您想要来自 Subprocess 的东西(MOTW)。

我使用这样的代码来调用 shell:

from subprocess import Popen, PIPE

## shell out, prompt
def shell(args, input_=''):
    ''' uses subprocess pipes to call out to the shell.
    
    args:  args to the command
    input:  stdin
    
    returns stdout, stderr
    '''
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate(input=input_)
    return stdout, stderr

关于python - 在 Python 脚本中包装交互式命令行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1567371/

相关文章:

python - 如何备份/恢复 python virtualenv?

python - 如何在机器人框架内运行sikuli脚本?

python - keras conv1d输入数据 reshape

perl - 我可以在shebang行中将perl的开关与/bin/env一起使用吗?

linux - 长标志后面可以跟一个字符吗?

python - 使用 `scipy.optimize.minimize` 最小化具有大梯度的函数

python - 在 Python 中向数组追加行,String 对象没有属性追加

java - 如何使用 spark-submit 将参数/属性传递给 Spark 作业

c - 从 C 运行命令行

c# - 如何绕过命令行长度限制?