I want understand what is the most efficient (speed and performance) way to read a gzip file backwards without loading the contents of the entire file in to memory.
Here is what I do currently, but is not efficient for really large files:
file = 'huge_file.log.gz'
import gzip
if file.endswith('gz'):
f = gzip.open(file)
# reverse the file contents
reverse_file_list = reversed(f.read().decode('utf-8').split('\n'))
I see there are some solutions in stackoverflow and codestate which do negative seeks but negative seeks are not supported when files are opened in binary mode as with gzip.open
Links: Most efficient way to search the last x lines of a file in python
http://code.activestate.com/recipes/439045/
So the solution fails for what I want to accomplish.