Compress and Save File with ZipFile

  • Share this:

Code introduction


This function takes a file path and an output path, using the built-in zipfile library to compress the specified file and save it to the specified output path.


Technology Stack : zipfile library

Code Type : File compression

Code Difficulty : Intermediate


                
                    
def zip_file(file_path, output_path):
    import zipfile
    with zipfile.ZipFile(output_path, 'w') as zipf:
        zipf.write(file_path)