Skip to content

Instantly share code, notes, and snippets.

@marcelom
Created October 7, 2016 18:37
Show Gist options
  • Save marcelom/cb96d569a54106e9c4dc8b9de4efd244 to your computer and use it in GitHub Desktop.
Save marcelom/cb96d569a54106e9c4dc8b9de4efd244 to your computer and use it in GitHub Desktop.
MMAP'ed tail in python
# Quick & Dirty... Full of bugs... Dont use for production code... You have been warned... ;-)
import mmap
import os
NUM_LINES=10
f=open('.bash_history', 'rb')
filesize = os.fstat(f.fileno()).st_size
mm =mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
pointer = filesize-1
if mm[pointer]=='\n':
pointer=pointer-1
for i in range(NUM_LINES):
while mm[pointer]!='\n':
pointer=pointer-1
pointer=pointer-1
pointer=pointer+1
print mm[pointer:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment