在 Python 中将多个文件夹(在目录内)压缩到单个文件(在新目录中)

时间:2021-02-12 06:45:08

标签: python python-3.x zip zipfile

我正在尝试将多个文件夹(在单个目录中)压缩到它们各自的文件(而不是一个大的压缩文件中),在一个新目录中(与原始文件夹具有相同的名称 + '.zip')。< /p>

我对 Python 很陌生,经过一番搜索,我想出了以下代码(在此视频 https://www.youtube.com/watch?v=MjH5UFhDnnY&ab_channel=WebDevPro 的帮助下):

import os, zipfile

os.chdir(r"C:\This\Is\An\Example")

for x in os.listdir():
    handle = zipfile.ZipFile(x + ".zip", "w")
    handle.write(x, compress_type = zipfile.ZIP_DEFLATED)
    handle.close()

我遇到的三个问题是,首先,我不确定如何将 output/final_product 设置为新目录。其次,最终的压缩文件是空的 - 这是因为原始文件包含其他文件,并且脚本将原始文件中的这些文件更改为新名称(Folder001.zip) - 如何在第一个/主要位置停止脚本文件夹,所以它不会进入子文件夹。最后,循环似乎最多可遍历文件夹 3 次(请参阅示例):

Folder001.zip

Folder001.zip.zip

Folder002.zip

Folder002.zip.zip

Folder002.zip.zip.zip

文件夹A.zip

文件夹A.zip.zip

文件夹A.zip.zip.zip

文件夹B.zip

FolderB.zip.zip

1 个答案:

答案 0 :(得分:0)

在您的情况下,最好的方法可能是遍历路径中的所有目录,并为每个目录创建一个 zip 文件,仅在这些目录中添加文件(如果我理解您的问题)。

所以,第一个问题是找到根文件夹中的所有目录。这可以通过在根文件夹上使用 os.walk 并查看其第一个输出的第二个元素来实现,即根目录中所有文件夹的列表。

然后,您必须为每个文件创建一个 zip 存档,其中仅包含这些子文件夹的文件。同样,您可以对每个文件夹使用 os.walk,但这次查看其第一个输出的第三项,它对应于该文件夹中的文件列表。

最好将问题分成两个不同的子问题:循环根文件夹和从文件夹创建一个 zip 文件。

以下代码段显示了循环遍历根文件夹并为输出 zip 文件创建路径的函数的可能实现:

def zip_subfolders(input_folder, output_folder):
    os.makedirs(output_folder, exist_ok=True)  # create output folder if it does not exist
    for folder_name in next(os.walk(input_folder))[1]:  # loop over all the folders in your input folder
        zipped_filepath = os.path.join(output_folder, f'{folder_name}.zip')  # create the path for the output zip file for this folder
        curr_folder_path = os.path.join(input_folder, folder_name)  # get the full path of the current folder
        create_zip(curr_folder_path, zipped_filepath)  # create the zip file and put in the right location

以下函数压缩每个子文件夹,添加位于文件夹第一级的所有文件:

def create_zip(folder_path, zipped_filepath):
    zip_obj = zipfile.ZipFile(zipped_filepath, 'w')  # create a zip file in the required path
    for filename in next(os.walk(folder_path))[2]: # loop over all the file in this folder
        zip_obj.write(
            os.path.join(folder_path, filename),  # get the full path of the current file
            filename,  # file path in the archive: we put all in the root of the archive
            compress_type=zipfile.ZIP_DEFLATED
        )
    zip_obj.close()

将所有内容放在一个脚本中:

import os
import zipfile

INPUT_FOLDER = 'to_zip'
OUTPUT_FOLDER = 'zipped'

def create_zip(folder_path, zipped_filepath):
    zip_obj = zipfile.ZipFile(zipped_filepath, 'w')  # create a zip file in the required path
    for filename in next(os.walk(folder_path))[2]: # loop over all the file in this folder
        zip_obj.write(
            os.path.join(folder_path, filename),  # get the full path of the current file
            filename,  # file path in the archive: we put all in the root of the archive
            compress_type=zipfile.ZIP_DEFLATED
        )
    zip_obj.close()

def zip_subfolders(input_folder, output_folder):
    os.makedirs(output_folder, exist_ok=True)  # create output folder if it does not exist
    for folder_name in next(os.walk(input_folder))[1]:  # loop over all the folders in your input folder
        zipped_filepath = os.path.join(output_folder, f'{folder_name}.zip')  # create the path for the output zip file for this folder
        curr_folder_path = os.path.join(input_folder, folder_name)  # get the full path of the current folder
        create_zip(curr_folder_path, zipped_filepath)  # create the zip file and put in the right location

if __name__ == '__main__':
    zip_subfolders(INPUT_FOLDER, OUTPUT_FOLDER)

相关问题