本文共 377 字,大约阅读时间需要 1 分钟。
import zipfile
import os
startdir = './test/neg'
file_news = './test_neg.zip' z = zipfile.ZipFile(file_news, 'w',zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk(startdir): fpath = dirpath.replace(startdir, '') fpath = fpath and fpath + os.sep or '' for filename in filenames: z.write(os.path.join(dirpath, filename), fpath + filename) print( '压缩成功') z.close()转载地址:http://hefgx.baihongyu.com/