Skip to content

Instantly share code, notes, and snippets.

@taojy123
Created August 29, 2016 08:02
Show Gist options
  • Save taojy123/cc24a871d8a19fa18f7459cd1cfa4691 to your computer and use it in GitHub Desktop.
Save taojy123/cc24a871d8a19fa18f7459cd1cfa4691 to your computer and use it in GitHub Desktop.
BackwardsReader
# Copyright (c) Peter Astrand <[email protected]>
class BackwardsReader:
"""Read a file line by line, backwards"""
BLKSIZE = 4096
def readline(self):
while 1:
newline_pos = string.rfind(self.buf, "\n")
pos = self.file.tell()
if newline_pos != -1:
# Found a newline
line = self.buf[newline_pos+1:]
self.buf = self.buf[:newline_pos]
if pos != 0 or newline_pos != 0 or self.trailing_newline:
line += "\n"
return line
else:
if pos == 0:
# Start-of-file
return ""
else:
# Need to fill buffer
toread = min(self.BLKSIZE, pos)
self.file.seek(-toread, 1)
self.buf = self.file.read(toread) + self.buf
self.file.seek(-toread, 1)
if pos - toread == 0:
self.buf = "\n" + self.buf
def __init__(self, file):
self.file = file
self.buf = ""
self.file.seek(-1, 2)
self.trailing_newline = 0
lastchar = self.file.read(1)
if lastchar == "\n":
self.trailing_newline = 1
self.file.seek(-1, 2)
for i in range(1000):
line = br.readline()
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment