Search code examples
pythonzip

How to zip each folder in the directory into a separate .zip archive in python?


For example, there is a directory D:\test\flo with 3 folders Test4, Test67, Test8. (each folder also has files)

As a result, we need to get in the directory D:\test\zip files Test4.zip, Test67.zip, Test8.zip

I tried to use the zipfile module and os. I do not understand how to do: The name of the archive is the name of the folder from which I take the file and how to write the desired file there


Solution

  • Given a directory as follows, where archive.py will save the contents of directories under flo into a sibling directory test, creating it if it doesn't exist:

    C:.
    │   archive.py
    │
    └───flo
        │   file7.txt
        │
        ├───Test4
        │   │   file1.txt
        │   │   file2.txt
        │   │
        │   └───somedir
        │           file.txt
        │
        ├───Test67
        │       file3.txt
        │       file4.txt
        │
        └───Test8
                file5.txt
                file6.txt
    

    archive.py

    import zipfile
    import pathlib
    import os
    
    # Create the test directory if needed
    os.makedirs('test', exist_ok=True)
    
    # Iterate directories under "flo"
    for directory in pathlib.Path ('flo').glob ('*/'):
        # Open test\<directory>.zip for writing
        with zipfile.ZipFile(pathlib.Path('test') / directory.with_suffix('.zip').name,
                             'w',
                             compression=zipfile.ZIP_DEFLATED,  # zlib compression
                             compresslevel=9) as zfile:         # maximum compression
            # Recursively list everything under <directory>
            for item in directory.rglob('*'):
                # Write each item into the archive, using the path relative to <directory>
                # as the name in the archive.
                zfile.write(item, arcname=item.relative_to(directory))
    

    Note that file7.zip is not a directory under flo, so won't be archived in this implementation.

    Result:

    C:.
    │   archive.py
    │
    ├───flo
    │   │   file7.txt
    │   │
    │   ├───Test4
    │   │   │   file1.txt
    │   │   │   file2.txt
    │   │   │
    │   │   └───somedir
    │   │           file.txt
    │   │
    │   ├───Test67
    │   │       file3.txt
    │   │       file4.txt
    │   │
    │   └───Test8
    │           file5.txt
    │           file6.txt
    │
    └───test
            Test4.zip
            Test67.zip
            Test8.zip
    

    The .zip files contain all the content under the corresponding flo directory.