python - 使用 python 将输入发送到使用子进程打开的程序?

标签 python linux

我有以下 python 代码,只想在终端询问特定问题时向终端发送命令。这是我到目前为止所拥有的

import subprocess
import sys
cmd = "Some application"
dat = str("")

p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, 
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE)

最佳答案

p.communicate(input="this is my input to the subprocess")

如果您需要捕获输出,请执行以下操作:

output = p.communicate(input="this is my input to the subprocess")[0]

关于python - 使用 python 将输入发送到使用子进程打开的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202490/

相关文章:

python - 使用未知数量的参数调用 python 函数

python - 创建空数据框的 Pandas 错误

python - 使用 selenium 将音频从文件传递到 Chrome

将文件内容发送到管道并一步计算 # 行的 Pythonic 方法

linux - MarkLogic rest 服务从选定的服务器返回 405 Method Not Allowed

linux - abas-ERP : Excecute a FO-Service-Program from a cronjob

linux - unix 中的 shell 脚本(在 bash shell 中),用于将文件从一个位置(即目录)复制到其他位置,根本不使用 cp 命令

linux - PHP-FPM 进程突然卡住

python - Python 的 file.write 是原子的吗?

c - 如何控制特定进程的 CPU 使用率?