reading and removing first x bytes of a file

Terry Reedy tjreedy at udel.edu
Thu Apr 29 13:00:21 EDT 2004


"Peter Hansen" <peter at engcorp.com> wrote in message
news:ULWdnWmpDt46vgzdRVn-gw at powergate.ca...
> Anyway, you can't actually "remove" bytes from the files, so
> what you really need to do is then read the *rest* of the bytes
> (i.e. keep the file open after the first read, and do a .read()
> of the rest of the data) and then write the shortened data to
> a temporary file (module tempfile can be helpful here), then
> once that's worked, use os.remove to remove the old file, and
> os.rename to rename the temp file to the same name as the old
> file.

IF the remainder of the file fits in memory, then it can 'moved up' by
rewriting same file after seeking to beginning of file.  Don't know how
safe on MAC OSes (ie, degree to which write is atomic).  Of course, if data
are important, one should have at least a few minutes of battery backup.

TJR







More information about the Python-list mailing list