pyzipfile - juedaiyuer/researchNote GitHub Wiki
zipfile
首先需要导入
import zipfile
读取一个zip文件,并打印压缩包里所有文件的名字;涉及到namelist方法
import zipfile
z =zipfile.ZipFile(filename, 'r')
# 这里的第二个参数用r表示是读取zip文件,w是创建一个zip文件
# z.namelist() 会返回压缩包内所有文件名的列表
for f in z.namelist():
print f
读取出z.namelist()中的第一个文件,并且输出到屏幕
print z.read(z.namelist()[0])
在tensorflow源代码中,位置为tensorflow/examples/tutorials/word2vec,读取text8.zip文件
word2vec_basic.py中的read_data()
# Read the data into a list of strings.
def read_data(filename):
"""Extract the first file enclosed in a zip file as a list of words."""
with zipfile.ZipFile(filename) as f:
data = tf.compat.as_str(f.read(f.namelist()[0])).split()
return data