如何使用 python 与 exe 文件交互
How to interact with an exe file with python
我有一个 exe 文件,它接受键盘输入,returns 一个基于输入文本的响应。当试图读取 exe 返回的输出时,python 脚本冻结。
我 运行 Windows 7 岁 python3.7。我已经在 continuously interacting with .exe file using python.
尝试过答案
from subprocess import Popen, PIPE
location = "C:\Users\file.exe"
p= Popen([location],stdin=PIPE,stdout=PIPE,stderr=PIPE, encoding="UTF8")
command='START'
p.stdin.write(command)
response=p.stdout.read()
我希望用输出文本填充响应,但程序却在该行冻结。
我要交互的exe文件是here(EMBRYO文件)
调用 p.stdout.readline()
而不是 p.stdout.read()
。这将一次给你一行,而不是等待进程关闭它的标准输出管道。有关更多信息,请阅读 documentation.
这里有一个更详细的例子。假设这个脚本是您的 exe 文件的替代品:
# scratch.py
from time import sleep
for i in range(10):
print('This is line', i)
sleep(1)
现在我启动该脚本,并尝试读取其输出:
from subprocess import Popen, PIPE, STDOUT
p = Popen(['python', 'scratch.py'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, encoding='UTF8')
for i in range(10):
response = p.stdout.read()
print('Response', i)
print(response)
结果如下所示:它等待十秒钟,然后打印出以下内容:
Response 0
This is line 0
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
Response 1
Response 2
Response 3
Response 4
Response 5
Response 6
Response 7
Response 8
Response 9
现在,如果我将其更改为 readline()
,我会得到:
Response 0
This is line 0
Response 1
This is line 1
Response 2
This is line 2
Response 3
This is line 3
Response 4
This is line 4
Response 5
This is line 5
Response 6
This is line 6
Response 7
This is line 7
Response 8
This is line 8
Response 9
This is line 9
似乎 stdout 没有被执行,因为 stdin 没有被刷新,在调用 p.stdin.flush() 之后一切正常!
from subprocess import Popen, PIPE
location = "C:\Users\file.exe"
p= Popen(location,stdin=PIPE,stdout=PIPE,stderr=PIPE, encoding="UTF8")
command='START\n'
p.stdin.write(command)
p.stdin.flush() # important
response=p.stdout.read()
感谢所有帮助过的人:)
我有一个 exe 文件,它接受键盘输入,returns 一个基于输入文本的响应。当试图读取 exe 返回的输出时,python 脚本冻结。
我 运行 Windows 7 岁 python3.7。我已经在 continuously interacting with .exe file using python.
尝试过答案from subprocess import Popen, PIPE
location = "C:\Users\file.exe"
p= Popen([location],stdin=PIPE,stdout=PIPE,stderr=PIPE, encoding="UTF8")
command='START'
p.stdin.write(command)
response=p.stdout.read()
我希望用输出文本填充响应,但程序却在该行冻结。
我要交互的exe文件是here(EMBRYO文件)
调用 p.stdout.readline()
而不是 p.stdout.read()
。这将一次给你一行,而不是等待进程关闭它的标准输出管道。有关更多信息,请阅读 documentation.
这里有一个更详细的例子。假设这个脚本是您的 exe 文件的替代品:
# scratch.py
from time import sleep
for i in range(10):
print('This is line', i)
sleep(1)
现在我启动该脚本,并尝试读取其输出:
from subprocess import Popen, PIPE, STDOUT
p = Popen(['python', 'scratch.py'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, encoding='UTF8')
for i in range(10):
response = p.stdout.read()
print('Response', i)
print(response)
结果如下所示:它等待十秒钟,然后打印出以下内容:
Response 0
This is line 0
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
Response 1
Response 2
Response 3
Response 4
Response 5
Response 6
Response 7
Response 8
Response 9
现在,如果我将其更改为 readline()
,我会得到:
Response 0
This is line 0
Response 1
This is line 1
Response 2
This is line 2
Response 3
This is line 3
Response 4
This is line 4
Response 5
This is line 5
Response 6
This is line 6
Response 7
This is line 7
Response 8
This is line 8
Response 9
This is line 9
似乎 stdout 没有被执行,因为 stdin 没有被刷新,在调用 p.stdin.flush() 之后一切正常!
from subprocess import Popen, PIPE
location = "C:\Users\file.exe"
p= Popen(location,stdin=PIPE,stdout=PIPE,stderr=PIPE, encoding="UTF8")
command='START\n'
p.stdin.write(command)
p.stdin.flush() # important
response=p.stdout.read()
感谢所有帮助过的人:)