read only the last x bytes from a text file

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Wed May 29 09:57:23 EDT 2002


On Wed, 29 May 2002 15:10:46 +0200, Felix Seeger wrote:
> Hi
> 
> I am writing a log monitor.
> It checks the filesize and the new filesize.
> 
> Now I want only the difference between this vars.
> eg read the last 1000 bytes from the file.
> 
> How can I do this ?
> 
> thanks
> have fun
> Felix

The usual way on Unixoid OS's is to read "past" EOF:

**************************************************************
import sys, time

fp = open("log.txt", "r")

while 1:
    line = fp.readline()
    if line:
        sys.stdout.write(line)
    else:
        time.sleep(1)

**************************************************************

Of course that's not real "polished"... call it proof of concept.

On Windoze, I don't know.





More information about the Python-list mailing list