Python - 如何将多个文件夹从路径压缩到单个 zip 文件?

Posted

技术标签:

【中文标题】Python - 如何将多个文件夹从路径压缩到单个 zip 文件?【英文标题】:Python - how to zip several folders from a path to a single zip file? 【发布时间】:2017-09-09 08:32:26 【问题描述】:

我有一个带有子文件夹 A、B、C、D、E、(...) 的 ROOT_FOLDER_PATH,我想用 Python 2.7 将文件夹 A、C 和 D 压缩到一个 zip 文件中,不包括所有其他文件夹和 ROOT_FOLDER_PATH 本身.我怎么做?感谢您的帮助。

【问题讨论】:

欢迎来到 SO。该站点不是代码编写服务,也不旨在提供完整的解决方案。当 SO 在这里帮助您解决特定的编程问题时,用户应该表现出一些努力和代码。你已经尝试过什么了吗? 对不起...我是 SO 或编程新手,大约 3 周前我开始自己学习 python,但我不知道这个(太棒了!)网站伦理。但我找到了一个基于另一个代码的解决方案,可以完全按照我的意愿行事。首先,我得到了可以压缩所有内容的东西(以前删除了我不想要的东西)。然后我开始“玩”逻辑和“if”、“and”、“or”、“not”、“in”,我已经达到了这一点。我不想被排除在外。谢谢。 【参考方案1】:

这比原来的问题更进一步。 提供从根文件夹中选择要压缩的目录的选项,以及排除所选目录中的子目录和文件的选项。

import os
import zipfile

def zipSelectExclude(src, dst, incluDIR1, incluDIR2, incluDIR3, excluSUBDIR1a, excluSUBDIR1b, excluSUBDIR3a, excluFILE3):
    zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print 'zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname)
            if incluDIR1 in dirname and excluSUBDIR1a not in dirname and excluSUBDIR1b not in dirname or incluDIR2 in dirname or incluDIR3 in dirname and excluSUBDIR3a not in dirname and excluFILE3 not in filename:
            zf.write(absname, arcname), 
    zf.close()

    """ Got the above from https://***.com/questions/27991745/python-zip-file-and-avoid-directory-structure  (answer from user 12321)
    and added the 'if' statement (and the 'def' modifications) to include selected folders from root folder to zip and exclude subdirs and files.
    Be careful with the 'if' statement hierarchical order - for each folder inclusion 'in dirname' specify it's subdirectories and file exclusion
    with 'and ... not in dirname' nx"""

def zipSE():
    src = os.path.join('/PATH/TO/MY', 'rootFOLDER')
    dst = os.path.join('/PATH/TO/SAVE/MY', 'ZIPfile')
    incluDIR1 = os.path.join(src, 'incluDIR1')
    incluDIR2 = os.path.join(src, 'incluDIR2')
    incluDIR3 = os.path.join(src, 'incluDIR3')
    excluSUBDIR1a = os.path.join(incluDIR1, 'excluSUBDIR1a')
    excluSUBDIR1b = os.path.join(incluDIR1, 'excluSUBDIR1b')
    excluSUBDIR3a = os.path.join(incluDIR3, 'excluSUBDIR3a')

    zipSelectExclude(src, dst, incluDIR1, incluDIR2, incluDIR3, excluSUBDIR1a, excluSUBDIR1b, excluSUBDIR3a, 'excluFILE3')

zipSE()

【讨论】:

【参考方案2】:

这是对原始问题的直接答案:

import os
import zipfile

def zipONLY(src, dst, incluDIR1, incluDIR2, incluDIR3):
    zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print 'zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname)
            """ Change the arguments and the 'if' statement to fit your needs"""
            if incluDIR1 in dirname or incluDIR2 in dirname or incluDIR3 in dirname:

            zf.write(absname, arcname),
    zf.close()

【讨论】:

【参考方案3】:

这是对面的:zip all except...

import os
import zipfile

def zipALLexcept(src, dst, excluDIR1, excluDIR2, excluDIR3):
    zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print 'zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname)

            """ Change the arguments and the 'if' statement to fit your needs."""
            if excluDIR1 not in dirname and excluDIR2 not in dirname and excluDIR3 not in dirname:

            zf.write(absname, arcname), 
    zf.close()

【讨论】:

以上是关于Python - 如何将多个文件夹从路径压缩到单个 zip 文件?的主要内容,如果未能解决你的问题,请参考以下文章

使用 Python 将内容从单个文件夹拆分到多个子文件夹

如何将多个提交合并到另一个分支上作为单个压缩提交?

golang 如何在Go中使用bzip2将多个文件压缩为单个文件

如何将mysql多个outfiles转换成单个压缩zip

在linux下如何将文件夹打包

如何获取从 iPhone 库中选择的多个图像的路径?