python - 如何在 Windows 上使用 python3 执行交互式 .exe 命令
问题描述
我想用python在windows上调用一个exe。如此调用的 exe 文件在内部处理某些内容,然后提示输入,一旦输入,则提示输入另一个输入。因此,我想将提示输入保留在 python 列表中,然后调用 exe。等待提示出现,然后提供列表中的第一个字符串,然后在第二个提示中提供列表中的第二个字符串。基本上我想在 python 中创建一个函数,它能够像Windows 上的期望一样运行。
尝试了此处提供的以下代码:通过 Python 与 Windows 控制台应用程序交互,但这似乎不再适用于 Windows 10:
from subprocess import *
import re
class InteractiveCommand:
def __init__(self, process, prompt):
self.process = process
self.prompt = prompt
self.output = ""
self.wait_for_prompt()
def wait_for_prompt(self):
while not self.prompt.search(self.output):
c = self.process.stdout.read(1)
if c == "":
break
self.output += c
# Now we're at a prompt; clear the output buffer and return its contents
tmp = self.output
self.output = ""
return tmp
def command(self, command):
self.process.stdin.write(command + "\n")
return self.wait_for_prompt()
p = Popen( ["cmd.exe"], stdin=PIPE, stdout=PIPE )
prompt = re.compile(r"^C:\\.*>", re.M)
cmd = InteractiveCommand(p, prompt)
listing = cmd.command("dir")
cmd.command("exit")
print(listing)
有人可以帮忙吗?
解决方案
推荐阅读
- html - Safari 中的 SVG 符号在 Chrome 和 Firefox 中移动时看起来很棒
- c# - 使用字符串激活表单(可变值)
- python - Selenium 在 Windows 10 中导致蓝屏错误
- angular - 在角度分量中使用布尔值
- pyqt5 - 我无法让 Qt.FramelessWindowHint 工作
- c# - 仅在 gridview 中显示当年的结果
- javascript - *ngFor 循环无法在 Angular 9 中显示 json 数据
- python - pubsub_v1 TypeError: __init__() 得到了一个意外的关键字参数“serialized_options”
- tensorflow - 如何使用 tf.data.Dataset.from_generator 读取音频文件
- powerbi - 计算列时的循环依赖