问题库 Python how-to

Python 如何解压 zip 文件

快速答案

用 zipfile.ZipFile extractall,先确认来源可信。

py
from zipfile import ZipFile
with ZipFile('dist.zip') as z:
    z.extractall('dist')

使用要点

  • 把输入目录和输出目录放到变量里,批量处理前先打印将要操作的文件列表。
  • 跨平台脚本优先使用 pathlib,避免手写路径分隔符。

相关内容