how to remove n bytes in a file?

Dasn dasn at bluebottle.com
Sat Sep 2 09:21:29 EDT 2006


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




More information about the Python-list mailing list