Tail a file in Python

Posted in python

Tail a file in Python

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:]
Posted by admica   @   20 October 2009
Tags : , , ,

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

Previous Post
« The future of the web
Next Post
How to debug wxPython errors when your applications crash »
Powered by Wordpress   |   Lunated designed by ZenVerse