用os.walk压缩文件夹

时间:2016-06-16 02:13:12

标签: python zipfile

我正在尝试使用os.walk()压缩文件夹和每个包含的子文件夹和文件,但是我无法删除根文件夹的文件夹路径 - 这意味着我想删除D:\\Users\\Username\\Desktop时打开zipfile,而是直接打开根文件夹。

我一直在尝试使用os.path.basename()和zipfile的arcname参数,但似乎无法做到正确:

def backupToZip(folder):

    import zipfile, os

    folder = os.path.abspath(folder) # make sure folder is absolute

    # Walk the entire folder tree and compress the files in each folder.  
    for foldername, subfolders, filenames in os.walk(folder):

        # Add the current folder to the ZIP file.
        backupZip.write(foldername)

        # Add all the files in this folder to the ZIP file.
        for filename in filenames:
            backupZip.write(os.path.join(foldername, filename))
    backupZip.close()

backupToZip('Sample Folder')

3 个答案:

答案 0 :(得分:1)

  1. 使用os.chdir更改当前路径
  2. 确保os.walk的参数是相对路径
  3. *使用os.chdir

    时要小心
    import zipfile, os
    
    def backupToZip(folder):
    
        cwdpath = os.getcwd() # save original path (*where you run this py file)
    
        saveToWhere = "tmp.zip"
        zf = zipfile.ZipFile(saveToWhere, mode='w')
    
        folder = os.path.abspath(folder) # make sure folder is absolute
        os.chdir(folder) # change to that absolute path
    
        # os.walk(relative_path)
        for foldername, subfolders, filenames in os.walk("./"):
            for filename in filenames:
                zf.write(os.path.join(foldername, filename))    
        zf.close()
    
        os.chdir(cwdpath) # back to original path
    

答案 1 :(得分:1)

如果您想避免影响整个过程的chdir,可以使用relpath从顶部文件夹开始获取相对路径。

您可以使用类似

的内容
def backupToZip(folder):

    import zipfile, os

    folder = os.path.abspath(folder) # make sure folder is absolute

    # Walk the entire folder tree and compress the files in each folder.  
    for foldername, subfolders, filenames in os.walk(folder):

        if foldername == folder:
             archive_folder_name = ''
        else:
             archive_folder_name = os.path.relpath(foldername, folder)

             # Add the current folder to the ZIP file.
             backupZip.write(foldername, arcname=archive_folder_name)

        # Add all the files in this folder to the ZIP file.
        for filename in filenames:
            backupZip.write(os.path.join(foldername, filename), arcname=os.path.join(archive_folder_name, filename))
    backupZip.close()

backupToZip('Sample Folder')

答案 2 :(得分:1)

基于user2313067上面的回答做了一些修改,最终得到了我想要的,以防有人好奇:

import zipfile, os

def backupToZip(folder):

    # Make sure folder is absolute.
    folder = os.path.abspath(folder) 

    backupZip = zipfile.ZipFile('backup.zip', 'w')

    backupZip.write(folder, arcname=os.path.basename(folder))


    # Walk the entire folder tree and compress the files in each folder.  
    for foldername, subfolders, filenames in os.walk(folder):

        # Add the current folder to the ZIP file if not root folder
        if foldername != folder:
            backupZip.write(foldername, arcname=os.path.relpath(foldername, os.path.dirname(folder)))

        # Add all the files in this folder to the ZIP file.
        for filename in filenames:
            backupZip.write(os.path.join(foldername, filename), arcname=os.path.join(os.path.relpath(foldername, os.path.dirname(folder)), filename))
    backupZip.close()