parsing a long text file for specific text

Alex Martelli aleax at aleax.it
Tue Jan 29 17:16:52 EST 2002


Jim Ragsdale wrote:

> Im a programming newbie, but I would like to write a python script to
> parse a possibly long log file for specific text and then dump some of the
> text to another file. The script would run every 30 minutes or so and i
> think it would be nice if it saved the last possition to keep from redoing
> everything. The only problem i see with saving the position is if the
> server crashes or gets restarted, the log file starts over, but I dont
> want to keep adding the same lines to the new file everytime.
> 
> Before if I have done anything like this, I used a loop to check to see if
> it matched a piece of text, but this takes a while. Is there a better way?
> Any thoughts? Any comments would be appreciated, thanks!

Assuming, for example, that each time the log file starts over it
starts with at least 40 bytes and in a way that differs from the
previous one within those first 40 bytes, it's not hard: when you're
done reading the logfile, save its first 40 bytes as well as its
length; when you restart, first get its first 40 bytes and compare
(just an ==, no loop needed), so you'll know whether to seek to
the previous length or just restart.


Alex




More information about the Python-list mailing list