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 file

Viewed 404
    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)

In the above script i have to put the condition i.e from the offset value 00000200 to 00000400 i have to take the data between offset values i.e 00000200 to 00000400 and store this extracted data to separate file. I am attaching the image of the input file. I need one pointer for opening the input file I need second pointer for writing the extracted data to a separate file.

input file

1 Answers

First of all, you are opening the files in binary mode, so reading and writing lines does not make (much) sense. You want read and write bytes.

This does what you want, I think:

>>> 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)

The interactive interpreter is a great way to explore the Python language. You can use the help() function to find out what you can and can't do with any functions and objects you pass to it. Have a look at help(open), help(input_file) and help(bytes_read) to better understand the above code snippet.

Related