python - 如何读取和执行二进制文件?
问题描述
我想从读取文件二进制文件中读取并执行二进制 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 运行程序,可以使用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)
推荐阅读
- javascript - JS:Fizz Buzz Sequence Quiz(将系列中的最后一个元素显示为整数,以防它等于 Fizz、Buzz 或 Fizz Buzz)
- scala - 如何将 RowMatrix 转换为 RDD[vector]?
- r - 计算单个日期列的持续时间(以秒为单位),而 R 中的 group_by ID
- azure - PowerApp 重置其他用户的密码
- vue.js - v-on 不能与动态创建的 v-for 一起使用
- kubernetes - GKE 节点升级“资源不足”
- javascript - 直接在 JS 中添加 HTML 标记和使用 document.create 向 DOM 添加元素的区别
- django - ModuleNotFound 错误 Django 和 Heroku 部署
- ios - 在swift中注册与MVC模式兼容的表单
- java - 这个对象是如何从它的接口创建的?