
[英]How to write a python script that can communicate with the input and output of a swift executable?
[英]How to get the Input and Output of an external Executable?
提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文。
我想深入挖掘自动化,但没有找到任何关于从任何可执行文件获取信息的信息。
我想要的是通过 Python 启动一个 exe,这样 python 就可以看到当我按下一个按钮或在该外部程序中执行其他操作时会发生什么。 有了给定的数据,我想自动化外部程序并给它数据来按下它自己的按钮。
就像 silenium,我知道代码并可以直接与之交互。
有什么办法吗?
我一直在寻找那种类型的自动化。 并降落在您可以使用子流程但无法找到一种方法来给我我想做的事情的地方,或者我只是没有做对。
您可以使用 Python 中的 subprocess 模块来启动外部可执行文件并与之交互。 要获取可执行文件的 output,您可以将 PIPE 参数与 subprocess.run 方法一起使用,并使用 stdout 属性检索 output。 例如:
import subprocess
result = subprocess.run(['executable.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = result.stdout.decode()
print(output)
要将输入发送到可执行文件,您可以将 stdin 参数与 subprocess.Popen 方法一起使用,并写入 Popen object 的 stdin 属性。例如:
import subprocess
process = subprocess.Popen(['executable.exe'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write(b'input data\n')
process.stdin.flush()
output = process.stdout.readline()
print(output)
请记住,与外部可执行文件的交互取决于可执行文件的设计方式,并且可能并非对所有可执行文件都可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.