在Python中自动向子流程发送响应

2024-05-29 05:11:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,当从Windows命令提示符运行时,需要我按enter键来回答许多问题。我试图从Python中自动运行程序并按enter键

我的尝试

import subprocess
from time import sleep

p = subprocess.Popen(['bcontroller','login','aws'], stdin=subprocess.PIPE, shell=True)
for _ in range(5):
    sleep(10)
    p.communicate(input=b'\n')

运行此操作时,首先会收到一条警告:

error: Incorrect function.

然后在大约10秒后,子进程抛出一个错误,停止执行:

ValueError: write to closed file

有人知道可能出了什么问题吗


Tags: fromimport程序awstimewindowsstdinlogin

热门问题