Zipfile leaves the last few lines from my file - why?
So I have a problem using a module zipfile
in Python. Currently, when I try to compress a KML file to create a new KMZ file, I am missing the last few lines. It doesn't seem to matter how long the KML is. I guess this is because the zipfile does not write the last zipped block.
kmz = zipfile.ZipFile(kmzPath , 'w')
kmz.write(kmlPath, 'CORS.kml', zipfile.ZIP_DEFLATED)
And yes, before asking, I imported zlib for compression. I have tried using zlib at a lower level but have the same problem. I am stuck.
Any ideas?
source to share
Make sure you call
kmz.close()
after the command .write(...)
, otherwise the complete contents of the file will not be flushed to disk. For this to happen automatically, always use a context manager with
, as the file will be closed when you exit the loop:
with zipfile.ZipFile(kmzPath, 'w') as kmz:
kmz.write(kmlPath, 'CORS.kml', zipfile.ZIP_DEFLATED)
source to share