我有兴趣通过 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/