2017-09-09 30 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() 
相關問題