Reputation: 503
When I create a zip file and try to open it in the same python code, why do I get BadZipFile error?
zip_file = "C:/Temp/tst_data_1022.txt"
filePath, fileName = os.path.split(zip_file)
baseFileName, fileExt = os.path.splitext(fileName)
destFtpFile = filePath + "/" + baseFileName + ".zip"
# Create the zip file and write to it
zFile = zipfile.ZipFile(destFtpFile, 'w', compression=zipfile.ZIP_DEFLATED, allowZip64=True)
zFile.write(zip_file, arcname=fileName)
# Read Zip file
zfile = zipfile.ZipFile(destFtpFile, 'r')
for name in zfile.namelist():
(dirname, filename) = os.path.split(name)
print "Decompressing " + filename
filename = "C:/Temp/" + filename
fd = open(filename,"w")
fd.write(zfile.read(name))
fd.close()
The zip file is created correctly. Error during reading: BadZipfile: File is not a zip file
Thanks,
Upvotes: 0
Views: 1329
Reputation: 154906
You are missing a call to zFile.close()
, which will flush the remaining data that needs to be written to the zip file, and close the underlying file descriptor.
Upvotes: 2