Skip to content

Instantly share code, notes, and snippets.

@davidbj
Forked from guixing/read_file_backwards.py
Created August 25, 2013 02:48
Show Gist options
  • Select an option

  • Save davidbj/6331682 to your computer and use it in GitHub Desktop.

Select an option

Save davidbj/6331682 to your computer and use it in GitHub Desktop.
def readfilerev(fd):
buff = 256
fd.seek(0,2)
size = fd.tell()
rem = size % buff
#pos = max(0, (size / buff -1) * buff)
line = ''
pos = -1
while fd.tell() > 0:
fd.seek(pos, 1)
d = fd.read(1)
if fd.tell() == 1:
fd.seek(-1,1)
yield d + line
else:
pos = -2
if d != '\n':
line = d+line
else:
if line:
yield line
line = d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment