I have a Python program which is going to take text files as input. However, some of these files may be gzip compressed.
Is there a cross-platform, usable from Python way to determine if a file is gzip compressed or not?
Is the following reliable or could an ordinary text file 'accidentally' look gzip-like enough for me to get false positives?
try:
gzip.GzipFile(filename, 'r')
# compressed
# ...
except:
# not compressed
# ...
Doesn’t seem to work well in python3...
returns (None, None) But from the unix command "File"
:~> file datasets/test datasets/test: gzip compressed data, was "iostat_collection", from Unix, last modified: Thu Jan 29 07:09:34 2015
The magic number for gzip compressed files is
1f 8b
. Although testing for this is not 100% reliable, it is highly unlikely that "ordinary text files" start with those two bytes—in UTF-8 it's not even legal.Usually gzip compressed files sport the suffix
.gz
though. Evengzip(1)
itself won't unpack files without it unless you--force
it to. You could conceivably use that, but you'd still have to deal with a possible IOError (which you have to in any case).One problem with your approach is, that
gzip.GzipFile()
will not throw an exception if you feed it an uncompressed file. Only a laterread()
will. This means, that you would probably have to implement some of your program logic twice. Ugly.gzip
itself will raise anOSError
if it's not a gzipped file.Can combine this approach with some others to increase confidence, such as checking the mimetype or looking for a magic number in the file header (see other answers for an example) and checking the extension.
Import the mimetypes module. It can automatically guess what kind of file you have, and if it is compressed.
i.e.
returns:
('text/plain', 'gzip')
"Is there a cross-platform, usable from Python way to determine if a file is gzip compressed or not?"
The accepted answer got me 90% of the way to the pretty reliable solution (test if first two bytes are
1f 8b
), but did not show how to actually do this in Python. Here is one possible way: