将多个特定文件夹压缩成一个zip文件

时间:2019-04-30 17:03:29

标签: python file zip directory

我有一堆充满数据的文件夹,我需要将特定的文件夹发送到zip文件夹。我可以使用批处理文件移动它们,但是它不会自动压缩,因此有冗余。我也尝试了所见的解决方案。

import os
import zipfile    

def zipit(folders, zip_filename):
    zip_file = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)

    for folder in folders:
        for dirpath, dirnames, filenames in os.walk(folder):
            for filename in filenames:
                zip_file.write(
                    os.path.join(dirpath, filename),
                    os.path.relpath(os.path.join(dirpath, filename), 
os.path.join(folders[0], '../..')))

    zip_file.close()


    folders = [
        "\Users\andria.baunee\Desktop\New folder",
        "\Users\andria.baunee\Desktop\New folder (2)",
        "\Users\andria.baunee\Desktop\New folder (3)"]


    zipit(folders, "\Users\andria.baunee\Desktop\hooray.zip")

似乎什么都没发生。

0 个答案:

没有答案