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
# ...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…