Files, Seek, and Readlines. A newbie question

Neil Hodgson nhodgson at bigpond.net.au
Mon Apr 1 15:37:26 EST 2002


John Purser:

> I'm using a large (>120,000 lines) sorted ASCII text file as an index.  I
> started running into problems as soon as I began to use the .seek() to
> position before a read.  Even when not opened as a binary file seek moves
by
> bytes not lines.  Is there a line oriented seek version?

   If you embed record numbers within the records then you could write a
binary search that would find a particular line without reading all the file
up to that point. Or if you know the average line length then start reading
at line / averageLineLength. These days computers come with lots of
memeory - just read the whole file into memory.

   Neil






More information about the Python-list mailing list