Compress File into Zip Archive

  • Share this:

Code introduction


This function compresses a specified file into a zip archive and saves it to the specified path.


Technology Stack : zipfile

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zip_file(file_path, destination):
    """
    Compress a file into a zip archive.

    Args:
        file_path (str): The path to the file to be compressed.
        destination (str): The path where the zip archive will be saved.
    """
    import zipfile
    with zipfile.ZipFile(destination, 'w') as zipf:
        zipf.write(file_path, arcname=file_path)                
              
Tags: