首页 > 解决方案 > 如何读取和执行二进制文件?

问题描述

我想从读取文件二进制文件中读取并执行二进制 calc.exe。

如何在以下脚本中运行 code1

file = r"calc.exe"
with open(file, "rb") as code1:
    exec(code1)

我不想直接执行 calc.exe 或在文件上写 code1 来执行

TypeError: exec() arg 1 must be a string, bytes or code object

标签: python

解决方案


要从 Python 运行程序,可以使用subprocess模块。

例子:

import subprocess

proc = subprocess.Popen(r"calc.exe")  # calc .exe needs to be availble in the working directory or from the path
outs, errs = proc.communicate(timeout=15)

推荐阅读