在Python中读取exe文件的执行结果,你可以使用`subprocess`模块。以下是使用`subprocess`模块读取exe文件执行结果的步骤:
1. 导入`subprocess`模块。
2. 设置exe文件的完整路径。
3. 使用`subprocess.run()`函数执行exe文件,并设置`capture_output=True`以捕获输出内容。
4. 通过`result.stdout`属性获取执行结果的输出。
5. 使用`decode()`方法将输出内容转换为可读的字符串。
下面是一个简单的示例代码:
import subprocess
设置exe文件的完整路径
exe_path = r'C:\path\to\example.exe'
使用subprocess.run()函数执行exe文件,并捕获输出内容
result = subprocess.run([exe_path], capture_output=True, text=True)
获取执行结果的输出
output = result.stdout
将输出内容转换为可读的字符串
output_str = output.decode('utf-8')
打印输出内容
print(output_str)
请确保替换`exe_path`变量的值为你的exe文件的实际路径。执行上述代码后,你将看到exe文件的输出结果