Search code examples
pythonfilecomparisonequality

Python compare contents of gz files


I have to compare a couple of pairs of gz files in Python. Idea is to run :

f1 = os.popen('file1.gz').read()
f2 = os.popen('file2.gz').read()
print(f1 == f2)

But this seems slow, since the whole content should be read. Is there another efficient way to check for equality of gz files in Python?


Solution

  • You can use the filecmp library.

    import filecmp
    
    print(filecmp.cmp('file1.gz', 'file2.gz'))
    

    Here is the documentation.