zip_path = "" password = "" extract_dir = os.path.dirname(zip_path) # 获取 zip 文件所在目录 with zipfile.ZipFile(zip_path, 'r') as zip_ref: #打开压缩包 zip_ref.extractall(path=extract_dir,pwd=password.encode()) #解压压缩包
Lv2.循环解压缩包套娃到当前目录(压缩包内仅有1个文件)。
1 2 3 4 5 6 7 8 9 10 11 12
import os import zipfile
filename = "" output_folder = "" while 1: with zipfile.ZipFile(filename, 'r') as zip_ref: #打开压缩包 zip_ref.extractall(extract_to) #解压压缩包 filename = zip_ref.namelist()[0] #获得压缩包内文件的名字 if 'zip' not in filename: break