How to read lines from end of a file?
Alexander Williams
thantos at chancel.org
Wed Dec 22 19:38:35 EST 1999
Two things, really:
> maxLen:: max number of bytes to read
This can be really, really problematic, especially if you don't know a
good value for it. Luckily, you trap for whether or not it is longer
than the whole file below, but unless you have a good idea how long
individual lines might be, it can be hard to ballpark a good number.
> # Dump the first line
> # It might be chopped off
> lines=fp.readlines()[1:]
So might the last line, especially if you catch it in the midst of a
non-atomic line write (say its constructed piecemeal then a CR
added). Somewhat rarer, but not impossible. Testing to see if the
last Char is a CR might be in order.
--
Alexander Williams (thantos at gw.total-web.net) | In the End,
"Join the secret struggle for the soul of the world." | Oblivion
Nobilis, a new Kind of RPG | Always
http://www.chancel.org | Wins
More information about the Python-list
mailing list