python实现压缩文件夹

python实现压缩文件夹,第1张

新建ziputil.py
import os
import zipfile

def write_zip_file(output_path, output_name, *input_path):
    """
    压缩文件
    :param output_path: 输出的路径
    :param output_name: 压缩包名称
    :param input_path: 压缩的文件夹路径
    :return:
    """
    dir = os.path.dirname(output_path + os.sep)
    if not os.path.exists(dir): os.makedirs(dir)
    with zipfile.ZipFile(str(output_path) + os.sep + output_name, 'w', zipfile.ZIP_DEFLATED) as f:
        for path in input_path:
            for root, dirs, files in os.walk(path):
                parent_path = os.path.dirname(path)
                for file in files:
                    # 指定压缩工作目录
                    os.chdir(parent_path)
                    file_path = str(root).replace(parent_path, '').lstrip(os.sep) + os.sep + file
                    # 压缩文件
                    f.write(file_path)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/873775.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存