文件压缩和解压缩 - zipfile模块

2020-12-29 15:53:36 浏览数 (1)

代码语言: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)  

0 人点赞