Python zlib-inflate alternative

I have a compressed file which I could uncompress on ubuntu command prompt using zlib-flate as below,

zlib-flate -uncompress < inputfile > outfile

Here inputfile is a compress file and outfile is the uncompressed version.

The compress file has a byte data.

I did not find the way to do the same using Python.

Please advise.

Answer

If the entire file fits in memory, zlib can do exactly this in a very straight forward manner;

import zlib

with open("input_file", "rb") as input_file:
   input_data = input_file.read()

decompressed_data = zlib.decompress(input_data)

with open("output_file", "wb") as output_file:
   output_file.write(decompressed_data)

If the file is too large to fit in memory, you may want to instead use zlib.decompressobj() which can do streaming but isn’t quite as straight forward.