How to create a zip archive of a directory in Python?

后端 未结 25 2940
暗喜
暗喜 2020-11-22 07:12

How can I create a zip archive of a directory structure in Python?

25条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 08:10

    So many answers here, and I hope I might contribute with my own version, which is based on the original answer (by the way), but with a more graphical perspective, also using context for each zipfile setup and sorting os.walk(), in order to have a ordered output.

    Having these folders and them files (among other folders), I wanted to create a .zip for each cap_ folder:

    $ tree -d
    .
    ├── cap_01
    |    ├── 0101000001.json
    |    ├── 0101000002.json
    |    ├── 0101000003.json
    |
    ├── cap_02
    |    ├── 0201000001.json
    |    ├── 0201000002.json
    |    ├── 0201001003.json
    |
    ├── cap_03
    |    ├── 0301000001.json
    |    ├── 0301000002.json
    |    ├── 0301000003.json
    | 
    ├── docs
    |    ├── map.txt
    |    ├── main_data.xml
    |
    ├── core_files
         ├── core_master
         ├── core_slave
    

    Here's what I applied, with comments for better understanding of the process.

    $ cat zip_cap_dirs.py 
    """ Zip 'cap_*' directories. """           
    import os                                                                       
    import zipfile as zf                                                            
    
    
    for root, dirs, files in sorted(os.walk('.')):                                                                                               
        if 'cap_' in root:                                                          
            print(f"Compressing: {root}")                                           
            # Defining .zip name, according to Capítulo.                            
            cap_dir_zip = '{}.zip'.format(root)                                     
            # Opening zipfile context for current root dir.                         
            with zf.ZipFile(cap_dir_zip, 'w', zf.ZIP_DEFLATED) as new_zip:          
                # Iterating over os.walk list of files for the current root dir.    
                for f in files:                                                     
                    # Defining relative path to files from current root dir.        
                    f_path = os.path.join(root, f)                                  
                    # Writing the file on the .zip file of the context              
                    new_zip.write(f_path) 
    

    Basically, for each iteration over os.walk(path), I'm opening a context for zipfile setup and afterwards, iterating iterating over files, which is a list of files from root directory, forming the relative path for each file based on the current root directory, appending to the zipfile context which is running.

    And the output is presented like this:

    $ python3 zip_cap_dirs.py
    Compressing: ./cap_01
    Compressing: ./cap_02
    Compressing: ./cap_03
    

    To see the contents of each .zip directory, you can use less command:

    $ less cap_01.zip
    
    Archive:  cap_01.zip
     Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
    --------  ------  ------- ---- ---------- ----- --------  ----
      22017  Defl:N     2471  89% 2019-09-05 08:05 7a3b5ec6  cap_01/0101000001.json
      21998  Defl:N     2471  89% 2019-09-05 08:05 155bece7  cap_01/0101000002.json
      23236  Defl:N     2573  89% 2019-09-05 08:05 55fced20  cap_01/0101000003.json
    --------          ------- ---                           -------
      67251             7515  89%                            3 files
    

提交回复
热议问题