Python zip multiple directories in one zip file

I have a ds237 top directory that has several subdirectories beneath it, like ds237 below:

 ds237/ ├── dataset_description.json ├── derivatives ├── sub-01 ├── sub-02 ├── sub-03 ├── sub-04 ├── sub-05 ├── sub-06 ├── sub-07 ├── sub-08 ├── sub-09 ├── sub-10 ├── sub-11 ├── sub-12 ├── sub-13 ├── sub-21 ├── sub-22 ├── sub-23 ├── sub-24 ├── sub-25 ├── sub-26 ├── sub-27 ├── sub-28 ├── sub-29 

I am trying to create several zip files (with the correct zip names) from ds237 according to the size of the zip files. sub01-01.zip: contain sub-01 to sub-07 sub08-13.zip: it contains sub08 to sub-13

I wrote a logic that creates a list of subdirectories [sub-01,sub-02, sub-03, sub-04, sub-05] . I created the list so that the total size of all subdirectories in the list does not exceed 5 GB.

My question is: how can I write a function to archive these subdirectories (which are in the list) into the target zip file with the correct name. Basically I want to write a function as follows:

def zipit([list of subdirs], 'path/to/zipfile/sub*-*.zip'):

On Linux, I usually achieve this:

 'zip -r compress/sub01-08.zip ds237/sub-0[1-8]' 
+5
source share
3 answers

By looking at fooobar.com/questions/28551 / ... , you can reuse this response function to add a directory to the 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-allocated list and the given name. You can use string formatting if you want to use a program name (for example, "path/to/zipfile/sub{}-{}.zip".format(start, end) ).

+9
source

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

+1
source

Next, you will get a zip file with the first ds100 folder:

 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 will have a structure similar to:

 ds100 ├── sub-01 | ├── 1 | ├── 2 | ├── 1 | ├── 2 ├── sub-02 ├── 1 ├── 2 ├── 1 ├── 2 
+1
source

Source: https://habr.com/ru/post/1271820/


All Articles