代码语言:javascript复制
from zipfile import ZipFile
with ZipFile("newZipped.zip", "w") as myzip:
myzip.write(r"eggs.txt") # 写入到压缩文件
myzip.write(r"新建 Microsoft Excel 97-2003 工作表 (3).xls") # 可以压缩多个文件
print(myzip.namelist())
with ZipFile("newZipped.zip", "r") as myzip:
# 可逐个解压
for member in myzip.infolist():
print(member)
# 全部解压到指定文件夹(文件夹不存在则会先创建)
myzip.extract(member, path="extracted", pwd=None) # pwd 为解压密码
# 也可全部解压到指定文件夹
# myzip.extractall(path="extracted", pwd=None)