使用python提取文件中的某些偏移量

时间:2013-01-25 07:17:30

标签: python

我正在尝试学习python,但我已经走到了一堵砖墙。

我只想提取.bin文件中的某些偏移量。 我有一个长度为“00FFFFF0”的bin文件

假设我想从块大小为“0x800”的“0x3F000”中提取,然后将其放入文件中我将如何处理它?我还没有任何代码,我希望我会得到一些好的输入。我是python的初学者(已经做了几个月),并且想学习如何真正做到这一点,仅仅是出于教育目的。

但重点是我希望能够提取特定的(偏移量;块大小) 我希望你明白我的意思。而且我非常感谢我给予的任何帮助。感谢

1 个答案:

答案 0 :(得分:2)

实际上是pretty self-explanatory

# Use the with statement to open a file so it will later be closed automatically
with open("in.bin", "rb") as infile:  # rb = read binary
    infile.seek(0x3F000, 0)           # 0 = start of file, optional in this case
    data = infile.read(0x800)

with open("out.bin", "wb") as outfile:
    outfile.write(data)