python zipfile 打包文件夹,压缩文件夹为zip包

def zip_dir(dir_path, out_full_name):
    """
    压缩指定文件夹
    :param dir_path: 目标文件夹路径
    :param out_full_name: 压缩文件保存路径 xxxx.zip
    :return: 无
    """
    zip = zipfile.ZipFile(out_full_name, "w", zipfile.ZIP_DEFLATED)
    if os.path.isdir(dir_path):
        for path, dirnames, filenames in os.walk(dir_path):
            for filename in filenames:
                zip.write(os.path.join(path, filename), os.path.join(path, filename))
    else:
        zip.write(dir_path, dir_path)
    zip.close()

你可能感兴趣的:(python)