title: How to efficiently read the last line of very big gzipped log file?
body: <p>I'd like to get the last line from a big gzipped log file, <strong>without having to iterate on all other lines</strong>, because it's a big file.</p>
  <p>I have read <a href="https://stackoverflow.com/questions/37227909/print-last-line-of-file-read-in-with-python">Print Last Line of File Read In with Python</a> and in particular <a href="https://stackoverflow.com/a/68413780/1422096">this answer</a> for big files, but it does not work for gzipped file. Indeed, I tried:</p>
  <pre><code>import gzip
  
  with gzip.open(f, 'rb') as g:
      g.seek(-2, os.SEEK_END) 
      while g.read(1) != b'\n':  # Keep reading backward until you find the next break-line
          g.seek(-2, os.SEEK_CUR) 
      print(g.readline().decode())
  </code></pre>
  <p>but it already takes more than 80 seconds for a 10 MB compressed / 130 MB decompressed file, on my very standard laptop!</p>
  <p><strong>Question: how to seek efficiently to the last line in a gzipped file, with Python?</strong></p>
  <hr />
  <p>Side-remark: if not gzipped, this method is very fast: 1 millisecond for a 130 MB file:</p>
  <pre><code>import os, time
  t0 = time.time()
  with open('test', 'rb') as g:
      g.seek(-2, os.SEEK_END) 
      while g.read(1) != b'\n': 
          g.seek(-2, os.SEEK_CUR) 
      print(g.readline().decode())
  print(time.time() - t0)    
  </code></pre>