【问题标题】:Python script which opens the exe file and copy the data from exe file based on offset condition and writing the extracted data to other filePython脚本,它打开exe文件并根据偏移条件从exe文件复制数据并将提取的数据写入其他文件
【发布时间】:2021-11-14 00:44:29
【问题描述】:
with open('C:\\users\desktop\Jhansi\parsing\out.exe', 'rb') as input_file:
with open('output.bin','wb') as output_file
for line in input_file:
output_file. Write(line)
在上面的脚本中,我必须输入条件,即从偏移值 00000200 到 00000400,我必须获取偏移值之间的数据,即 00000200 到 00000400 并将提取的数据存储到单独的文件中。我正在附加输入文件的图像。
我需要一个指针来打开输入文件
我需要第二个指针来将提取的数据写入单独的文件。
input file
【问题讨论】:
标签:
python
parsing
conditional-statements
extract
offset
【解决方案1】:
首先,您以二进制模式打开文件,因此读取和写入行没有(太多)意义。您需要读取和写入字节。
我想这就是你想要的:
>>> with open('some_pathname', 'rb') as input_file:
... input_file.seek(offset_start)
... num_bytes = offset_end - offset_start
... bytes_read = input_file.read(num_bytes)
>>> with open('another_pathname', 'wb') as output_file:
... output_file.write(bytes_read)
交互式解释器是探索 Python 语言的好方法。您可以使用help() 函数来找出您可以对传递给它的任何函数和对象做什么和不能做什么。看看help(open)、help(input_file) 和help(bytes_read) 可以更好地理解上面的代码sn-p。