Merging Two Files into a Single Zipped File

  • Share this:

Code introduction


This function merges the contents of two files and writes the combined content to a new file.


Technology Stack : File handling

Code Type : File processing

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()
        zipped_data = data1 + data2
        with open('zipped_file', 'wb') as zipped_f:
            zipped_f.write(zipped_data)                
              
Tags: