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

John Lenton john at
Sat Jan 15 00:11:58 EST 2005

On Wed, Jan 12, 2005 at 10:36:54PM -0800, yaipa 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.

ok, after having read the answers, I feel I must, once again, bring
mmap into the discussion. It's not that I'm any kind of mmap expert,
that I twirl mmaps for a living; in fact I barely have cause to use it
in my work, but give me a break!  this is the kind of thing mmap
*shines* at!

Let's say m is your mmap handle, a is the pattern you want to find,
b is the pattern you want to replace, and n is the size of both a and

You do this:

  p = m.find(a)
  m[p:p+n] = b

and that is *it*. Ok, so getting m to be a mmap handle takes more work
than open() (*) A *lot* more work, in fact, so maybe you're justified
in not using it; some people can't afford the extra

  s = os.stat(fn).st_size
  m = mmap.mmap(f.fileno(), s)

and now I'm all out of single-letter variables.

*) why isn't mmap easier to use? I've never used it with something
other than the file size as its second argument, and with its access
argument in sync with open()'s second arg.

John Lenton (john at -- Random fortune:
If the aborigine drafted an IQ test, all of Western civilization would
presumably flunk it.
		-- Stanley Garn
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <>

More information about the Python-list mailing list