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

时间:2017-09-09 08:32:26

标签: python-2.7 directory zipfile

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

3 个答案:

答案 0 :(得分:0)

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

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://stackoverflow.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()

答案 1 :(得分:0)

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

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()

答案 2 :(得分:0)

这是oposite:zip all除了......

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()