parsing a long text file for specific text

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Jan 29 12:56:59 EST 2002


"Jim Ragsdale" <overlord at netdoor.com> writes:

> 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.

The easiest way to allow for that scenario is that the restart process
of the server ought to remove your last-index file. If that fails, you
could use the file size of the new log file as an indication that the
log got truncated, and that you need to start from the
beginning. There is a slight chance that the server log has rolled
over and grown beyond your last position. To protect against this
case, you could save a few bytes just before the last position; if
those have changed, the log file has rolled over.

All of this is doable in pure Python - you just have to decide what
your algorithm should be, first.

HTH,
Martin



More information about the Python-list mailing list