Skip to content

Instantly share code, notes, and snippets.

@archerslaw
Forked from guixing/read_file_backwards.py
Last active December 21, 2015 16:09
Show Gist options
  • Save archerslaw/6331668 to your computer and use it in GitHub Desktop.
Save archerslaw/6331668 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#coding:utf-8
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