[Python-ideas] Add a .pop() method to ZipFile
Daniel Holth
dholth at gmail.com
Fri May 10 14:32:17 CEST 2013
Check out this efficient way to remove the last file from any ordinary zip file.
class TruncatingZipFile(zipfile.ZipFile):
"""ZipFile that can pop files off the end. This works for ordinary zip
files that do not contain non-ZIP data interleaved between the compressed
files."""
def pop(self):
"""Truncate the last file off this zipfile."""
if not self.fp:
raise RuntimeError(
"Attempt to pop from ZIP archive that was already closed")
last = self.infolist().pop()
del self.NameToInfo[last.filename]
self.fp.seek(last.header_offset, os.SEEK_SET)
self.fp.truncate()
self._didModify = True
More information about the Python-ideas
mailing list