python - 如何在 Linux shell 中更新二进制文件中间的一个字节?

标签 python linux cmp

当我在 2 个文件上运行 cmp 时,我得到一个字节的差异:

cmp -l file1.dmp_byte file2.dmp
913462  0 100

如何用值 100 更新文件 file1.dmp 的字节 913462?

可以使用标准的 Linux shell 工具或 Python 来完成吗?

最佳答案

在 Python 中,您可以使用内存映射文件:

import mmap
with open('file1.dmp', 'r+b') as fd:
    mm = mmap.mmap(fd.fileno(), 0)
    mm[913462] = chr(100)
    mm.close()

关于python - 如何在 Linux shell 中更新二进制文件中间的一个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44659819/

相关文章:

python - skimage.transform.rescale 将数据类型从 uint8 更改为 float64

python - 线性回归预测的非常大的值

python - 如何使 pexpect 命令在我的程序中没有 interact() 的情况下执行

linux - 没有加入用户时是否可以检查多播组列表?

Android:组件的 IntentFilters

python - Plotly:如何使用分组图例设置多个子图?

python - PyInstaller "failed to execute script"错误

linux - 将tomcat服务器端口更改为80不起作用

python - 使用 cmp 在 Python 中排序()

bash - CMP 如何将比较值传递到 bash 脚本中的下一行?