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

Jeff Shannon jeff at ccvcorp.com
Thu Jan 13 14:40:52 EST 2005


Bengt Richter wrote:

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

What happens when the bytes to be replaced are broken across a block 
boundary?  ISTM that neither half would be recognized....

I believe that this requires either reading the entire file into 
memory, to scan all at once, or else conditionally matching an 
arbitrary fragment of the end of a block against the beginning of the 
oldbinstring...  Given that the file in question is only a few tens of 
kbytes, I'd think that doing it in one gulp is simpler.  (For a large 
file, chunking it might be necessary, though...)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list