Python zip multiple directories into one zip file

前端 未结 3 1058
春和景丽
春和景丽 2021-01-13 05:11

I have a top directory ds237 which has multiple sub-directories under it as below:

ds237/
├── dataset_description.json
├── derivatives
├── sub-0         


        
相关标签:
3条回答
  • Looking at https://stackoverflow.com/a/1855118/375530, you can re-use that answer's function to add a directory to a ZipFile.

    import os
    import zipfile
    
    
    def zipdir(path, ziph):
        # ziph is zipfile handle
        for root, dirs, files in os.walk(path):
            for file in files:
                ziph.write(os.path.join(root, file),
                           os.path.relpath(os.path.join(root, file),
                                           os.path.join(path, '..')))
    
    
    def zipit(dir_list, zip_name):
        zipf = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
        for dir in dir_list:
            zipdir(dir, zipf)
        zipf.close()
    

    The zipit function should be called with your pre-chunked list and a given name. You can use string formatting if you want to use a programmatic name (e.g. "path/to/zipfile/sub{}-{}.zip".format(start, end)).

    0 讨论(0)
  • 2021-01-13 05:54

    You can use subprocess calling 'zip' and passing the paths as arguments

    0 讨论(0)
  • 2021-01-13 06:03

    The following will give you zip file with a first folder ds100:

    import os
    import zipfile    
    
    def zipit(folders, zip_filename):
        zip_file = zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED)
    
        for folder in folders:
            for dirpath, dirnames, filenames in os.walk(folder):
                for filename in filenames:
                    zip_file.write(
                        os.path.join(dirpath, filename),
                        os.path.relpath(os.path.join(dirpath, filename), os.path.join(folders[0], '../..')))
    
        zip_file.close()
    
    
    folders = [
        "/Users/aba/ds100/sub-01",
        "/Users/aba/ds100/sub-02",
        "/Users/aba/ds100/sub-03",
        "/Users/aba/ds100/sub-04",
        "/Users/aba/ds100/sub-05"]
    
    zipit(folders, "/Users/aba/ds100/sub01-05.zip")
    

    For example sub01-05.zip would have a structure similar to:

    ds100
    ├── sub-01
    |   ├── 1
    |       ├── 2
    |   ├── 1
    |   ├── 2
    ├── sub-02
        ├── 1
            ├── 2
        ├── 1
        ├── 2
    
    0 讨论(0)
提交回复
热议问题