truncating a file from the top down
Fredrik Lundh
fredrik at pythonware.com
Tue Mar 29 12:29:20 EST 2005
Mike Rovner wrote:
> if os.stat says the_file is too big:
> fh = open(the_file, 'rb')
> fh.seek(2008, 2)
should be
fh.seek(-2008, 2)
right?
> data = fh.read()
> fh.close()
> assert len(data)==2008 # you may want some error processing here
> fh = open(the_file, 'wb')
> fh.write(data)
> fh.close()
or
if os.path.getsize(the_file) > TOO_BIG:
fh = open(the_file, 'rb+')
fh.seek(-2008, 2)
data = fh.read()
fh.seek(0) # rewind
fh.write(data)
fh.truncate()
fh.close()
</F>
More information about the Python-list
mailing list