Compress File to Zip Format

  • Share this:

Code introduction


This function is used to compress a file at a specified path into a zip format and save it to a specified output path.


Technology Stack : os, zipfile

Code Type : File compression

Code Difficulty : Intermediate


                
                    
def zip_file(file_path, output_path):
    import os
    import zipfile

    def create_zip(output_path, file_path):
        with zipfile.ZipFile(output_path, 'w') as zipf:
            zipf.write(file_path, os.path.basename(file_path))

    create_zip(output_path, file_path)                
              
Tags: