使用 os.walk 压缩文件夹

Zipping a folder with os.walk

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

我一直在尝试使用 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')
  1. 使用os.chdir更改当前路径
  2. 确保os.walk的参数是相对路径

*使用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

如果您想避免影响整个过程的 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')

根据上面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()