Zipping Two Files into a Single Archive

  • Share this:

Code introduction


This function zips two files into a single zip file. If one or more files do not exist, it raises a FileNotFoundError exception.


Technology Stack : The packages and technologies used in the code[English]

Code Type : Function

Code Difficulty :


                
                    
def zipfiles(file1, file2, output):
    import os
    import zipfile

    def create_zip(output, files):
        with zipfile.ZipFile(output, 'w') as zipf:
            for file in files:
                zipf.write(file, os.path.basename(file))

    if os.path.exists(file1) and os.path.exists(file2):
        files_to_zip = [file1, file2]
        create_zip(output, files_to_zip)
    else:
        raise FileNotFoundError("One or more files do not exist.")