2017-09-09 29 views
-2

我有一个ROOT_FOLDER_PATH与子文件夹A,B,C,D,E(...),我想压缩文件夹A,C和D到一个压缩文件EXCLUDING所有其他和ROOT_FOLDER_PATH本身与Python 2.7。我怎么做?谢谢你的帮助。Python - 如何将几个文件夹从一个路径压缩到一个zip文件?

+1

欢迎SO。本网站不是代码编写服务,不适用于提供完整的解决方案。预计用户将展示一些努力和代码,而SO在此期间将帮助您解决具体的编程问题。你有没有尝试过任何东西? –

+0

对不起...我是新来的SO或编程,大约三个星期前我开始自学python,但我不知道这个(很棒!)网站道德。但是我发现了一个基于另一个代码的解决方案来完成我想要的功能。首先,我得到了一些能够拉伸所有东西的东西(之前删除了我不想要的东西)。然后,我开始用逻辑和“如果”,“和”,“或”,“不是”,“在”中“玩”,我已经达到了这个目标。我希望不要放弃。谢谢。 –

回答

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

这是oposite:压缩所有除...

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() 
相关问题