import zipfile
# 说明:
# 1、目录结构:D:/picture/year/month/111.jpg
# 2、需求:将图片按'月'为单位压缩到各自压缩包里,放入同一个文件夹
# D:/picture_zip/202101.zip
# D:/picture_zip/202102.zip ...
def batch_zip_file():
source_path = "D:/picture/"
zip_parent_path = "D:/picture_zip/"
years = os.listdir(source_path)
for year in years:
print(year)
year_path = source_path + year
months = os.listdir(year_path)
for month in months:
print(month)
month_path = year_path + "/" + month
pictures = os.listdir(month_path)
zf = zipfile.ZipFile(zip_parent_path + month + ".zip", 'w', zipfile.ZIP_DEFLATED)
for picture in pictures:
zf.write(monthe_path + "/" + picture, month+"/"+picture)
zf.close()