krlmlr krlmlr - 1 year ago 124
Python Question

Transparent decompression of stream

How do I read a file in Python that may or may not be gzip-compressed?

My current code

with"file.xml") as f:
xml.sax.parse(f, reader)

works with
files but not with

File "/usr/lib/python3.5/", line 409, in _read_gzip_header
raise OSError('Not a gzipped file (%r)' % magic)

Is there a substitute for the
call that always returns an uncompressed stream based on file contents and/or file extension?

The answers to the related question would solve my problem, but I'm looking for a packaged solution that doesn't involve any extra code.

Answer Source

Just use the function defined in this answer to a related question:

import gzip

def opener(filename):
    f = open(filename, 'rb')
    if ( == '\x1f\x8b'):
        return gzip.GzipFile(fileobj=f)
        return f

You can also extend it to support other file formats.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download