If your file is several gigabytes, then probably my solution will only apply to 64-bit operating systems:
from __future__ import with_statement import mmap, os def insert_string(fp, offset, some_bytes):
NB: this is a Python 2 program on Linux. YMMV.
source share