Alternating Block Compression of Two Files

  • Share this:

Code introduction


This function is used to compress the contents of two files alternately in block form, returning a generator that yields the contents of corresponding blocks of the two files each time.


Technology Stack : File read and write

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        while True:
            b1 = f1.read(10)
            b2 = f2.read(10)
            if not b1 or not b2:
                break
            yield b1 + b2                
              
Tags: