Compress File to Zip Format

  • Share this:

Code introduction


This function compresses a file at the specified path into a zip format and saves it to the specified output path.


Technology Stack : zipfile, os

Code Type : Function

Code Difficulty : Intermediate


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

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