![](/img/trans.png)
[英]Python openpyxl error: BadZipFile: File is not a zip file
[英]Unable to open zip file in Python - BadZipFile error
当我创建一个zip文件并尝试在同一个python代码中打开它时,为什么我会收到BadZipFile错误?
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()
zip文件已正确创建。 读取时出错:BadZipfile:文件不是zip文件
谢谢,
您缺少对zFile.close()
的调用,该调用将刷新需要写入zip文件的剩余数据,并关闭基础文件描述符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.