Merging Files into a ZIP Alternative

  • Share this:

Code introduction


This function merges the contents of two files alternately, writing the content of each file into a new ZIP file.


Technology Stack : Built-in file operations

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        with open('zipped_files.zip', 'wb') as f3:
            while True:
                block1 = f1.read(1024)
                block2 = f2.read(1024)
                if not block1 and not block2:
                    break
                f3.write(block1)
                f3.write(block2)