似乎是在敲我的新手错误,我不是新手。 我有一个1.2G已知良好的zipfile 'train.zip',其中包含一个3.5G文件'train.csv'。 我打开zip文件和文件本身没有任何例外(没有 LargeZipFile ),但生成的文件流似乎是空的。 (UNIX 'unzip -c ...'确认它很好) Python ZipFile.open()返回的文件对象不可搜索或无法识别,因此我无法检查。
Python发布是 2.7.3 EPD-free 7.3-1(32位);但对于大拉链应该没问题。操作系统是MacOS 10.6.6
import csv
import zipfile as zf
zip_pathname = os.path.join('/my/data/path/.../', 'train.zip')
#with zf.ZipFile(zip_pathname).open('train.csv') as z:
z = zf.ZipFile(zip_pathname, 'r', zf.ZIP_DEFLATED, allowZip64=True) # I tried all permutations
z.debug = 1
z.testzip() # zipfile integrity is ok
z1 = z.open('train.csv', 'r') # our file keeps coming up empty?
# Check the info to confirm z1 is indeed a valid 3.5Gb file...
z1i = z.getinfo(file_name)
for att in ('filename', 'file_size', 'compress_size', 'compress_type', 'date_time', 'CRC', 'comment'):
print '%s:\t' % att, getattr(z1i,att)
# ... and it looks ok. compress_type = 9 ok?
#filename: train.csv
#file_size: 3729150126
#compress_size: 1284613649
#compress_type: 9
#date_time: (2012, 8, 20, 15, 30, 4)
#CRC: 1679210291
# All attempts to read z1 come up empty?!
# z1.readline() gives ''
# z1.readlines() gives []
# z1.read() takes ~60sec but also returns '' ?
# code I would want to run is:
reader = csv.reader(z1)
header = reader.next()
return reader
答案 0 :(得分:15)
原因是:
的组合命令行解决方法是解压缩然后重新压缩,以获得普通的类型8:已放弃。
zipfile will throw an exception in 2.7,3.2+我认为由于法律原因,zipfile永远无法真正处理类型9。 Python文档没有提到zipfile不能handle other compression types :(
答案 1 :(得分:3)
我处理Python的ZipFile不支持的压缩类型的解决方案是在ZipFile.extractall失败时依靠对7zip的调用。
from zipfile import ZipFile
import subprocess, sys
def Unzip(zipFile, destinationDirectory):
try:
with ZipFile(zipFile, 'r') as zipObj:
# Extract all the contents of zip file in different directory
zipObj.extractall(destinationDirectory)
except:
print("An exception occurred extracting with Python ZipFile library.")
print("Attempting to extract using 7zip")
subprocess.Popen(["7z", "e", f"{zipFile}", f"-o{destinationDirectory}", "-y"])
答案 2 :(得分:1)
文件是否可能太大而python无法提取内存?当train.csv更小时它是否有效?
您可以尝试使用与此处所示类似的方法阅读:How do you unzip very large files in python?