title: How to tell if a file is gzip compressed?
body: <p>I have a Python program which is going to take text files as input.  However, some of these files may be gzip compressed.  </p>
  
  <p>Is there a cross-platform, usable from Python way to determine if a file is gzip compressed or not? </p>
  
  <p>Is the following reliable or could an ordinary text file 'accidentally' look gzip-like enough for me to get false positives?</p>
  
  <pre><code>try:
      gzip.GzipFile(filename, 'r')
      # compressed
      # ...
  except:
      # not compressed
      # ...
  </code></pre>