Merging Files into a Zip File

  • Share this:

Code introduction


This function merges the contents of two files and saves the combined contents into a new zip file.


Technology Stack : File reading and writing

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        data1 = f1.read()
        data2 = f2.read()
        combined = data1 + data2
        with open(file1 + '.zip', 'wb') as f3:
            f3.write(combined)