how to remove n bytes in a file?

Grant Edwards grante at visi.com
Sat Sep 2 09:59:19 EDT 2006


On 2006-09-02, Dasn <dasn at bluebottle.com> wrote:
> Hi.
> Suppose we have a very large file, and wanna remove 'n' bytes in the
> middle of the file. My thought is:
> 1, read() until we reach the bytes should be removed, and mark the
> position as 'pos'.
> 2, seek(tell() + n) bytes
> 3, read() until we reach the end of the file, into a variable, say 'a'
> 4, seek(pos) back to 'pos'
> 5, write(a)
> 6, truncate()
>
> If the file is really large, the performance may be a problem.
> Is there a clever way to finish? Could mmap() help? Thx

Instead of reading till the end of the file in step 3, do it a
chunk at a time in a loop.

-- 
Grant Edwards                   grante             Yow!  I'm gliding over a
                                  at               NUCLEAR WASTE DUMP near
                               visi.com            ATLANTA, Georgia!!



More information about the Python-list mailing list