Merging Files with Null Padding

  • Share this:

Code introduction


The function merges the contents of two files into a new file. If the files are of different lengths, the shorter file is padded with null bytes at the end.


Technology Stack : File reading and writing

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        bytes1 = f1.read()
        bytes2 = f2.read()
        zipped = bytes1 + bytes2
    with open('zipped_file', 'wb') as f:
        f.write(zipped)