Tail a file in Python
===Tail a file in Python===
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def tail( f, window=20 ): f.seek( 0, 2 ) bytes= f.tell() size= window block= -1 while size > 0 and bytes+block*1024 > 0: f.seek( block*1024, 2 ) data= f.read( 1024 ) linesFound= data.count('\n') size -= linesFound block -= 1 f.seek( block*1024, 2 ) f.readline() lastBlocks= list( f.readlines() ) print lastBlocks[-window:] |