Delete / Insert Data in mmap'ed File

I am working on a script in Python that maps a file for processing using mmap().

The tasks requires me to change the file's contents by

  1. Replacing data
  2. Adding data into the file at an offset
  3. Removing data from within the file (not whiting it out)

Replacing data works great as long as the old data and the new data have the same number of bytes:

VDATA = mmap.mmap(f.fileno(),0)
start = 10
end = 20
VDATA[start:end] = "0123456789"

However, when I try to remove data (replacing the range with "") or inserting data (replacing the range with contents longer than the range), I receive the error message:

IndexError: mmap slice assignment is wrong size

This makes sense.

The question now is, how can I insert and delete data from the mmap'ed file? Читая документацию, мне кажется, что я могу перемещать все содержимое файла вперед и назад, используя цепочку низкоуровневых действий, но я бы предпочел избежать этого, если есть более простое решение.

5
задан Brian Tompsett - 汤莱恩 2 December 2015 в 09:24
поделиться