finding/replacing a long binary pattern in a .bin file

Bengt Richter bokr at oz.net
Thu Jan 13 03:13:14 EST 2005


On Thu, 13 Jan 2005 16:51:46 +1000, Stephen Thorne <stephen.thorne at gmail.com> wrote:

>On 12 Jan 2005 22:36:54 -0800, yaipa <yaipa at yahoo.com> wrote:
>> What would be the common sense way of finding a binary pattern in a
>> .bin file, say some 200 bytes, and replacing it with an updated pattern
>> of the same length at the same offset?
>> 
>> Also, the pattern can occur on any byte boundary in the file, so
>> chunking through the code at 16 bytes a frame maybe a problem.  The
>> file itself isn't so large, maybe 32 kbytes is all and the need for
>> speed is not so great, but the need for accuracy in the
>> search/replacement is very important.
>
>Okay, given the requirements.
>
>f = file('mybinfile')
>contents = f.read().replace(oldbinstring, newbinstring)
>f.close()
>f = file('mybinfile','w')
>f.write(contents)
>f.close()
>
>Will do it, and do it accurately. But it will also read the entire
>file into memory.
>
You must be on linux or such, otherwise you would have shown opening the
_binary_ files (I assume that's what a .bin file is) with 'rb' and 'wb', IWT.

Not sure what system the OP was/is on.

BTW, I'm sure you could write a generator that would take a file name
and oldbinstring and newbinstring as arguments, and read and yield nice
os-file-system-friendly disk-sector-multiple chunks, so you could write

    fout = open('mynewbinfile', 'wb')
    for buf in updated_file_stream('myoldbinfile','rb', oldbinstring, newbinstring):
        fout.write(buf)
    fout.close()

(left as an exercise ;-)
(modifying a file "in place" is another exercise)
(doing the latter with defined maximum memory buffer usage
 even when mods increase the length of the file is another ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list