import zipfile
for path in ret:
print(path)
# 读取文件
# with open(path, 'r') as f:
with zipfile.ZipFile(path, "r") as myzip:
# with gzip.open(path, 'r') as f:
i = 1
first_file_name = myzip.namelist()[0]
f = myzip.open(first_file_name)
for line in f:
allname = line.decode().strip('\n')
myzip.close()
f.close()
读完一定要关闭,不然会占用很大的内存,也会造成不安全因素,虽然不close不会报错,但是这样搞不对。zipfile要先生成一个Zipfile对象,然后显示压缩包里的文件名称,然后用open函数读取文件名称再读内容。如果不用open函数直接for line in f:
就会报错说类型错误不可迭代TypeError: 'ZipFile' object is not iterable
。但是gzip不用,直接都进来循环就行了,下边是gzip读取方法。
if path.find(".gz") >= 0:
with gzip.open(path, 'r') as f:
for line in f:
# 文件中每行的数据
allname = line.decode().strip('\n')
f_out.close()
f_out.close()