Created
October 7, 2016 18:37
-
-
Save marcelom/cb96d569a54106e9c4dc8b9de4efd244 to your computer and use it in GitHub Desktop.
MMAP'ed tail in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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