[Tutor] Simple Question...

R. Alan Monroe amonroe at columbus.rr.com
Sun Oct 17 03:31:47 CEST 2004


> To do this without loading the file into memory, and without relying
> on wc (which ought to be very fast even with large files, if you need
> that), you could do:
> [snip]

>     for i in range(1, r): f.readline()

This seems like it would be awfully slow on a multimegabyte file,
force reading every line up to where you want to be.

> anybody have a faster implementation?

Does someone have a link to a neutral text file we can all use to test
with? I'm betting a blind seek right to the middle of the file is
going to blow the above away, but I want to try it. I'm on windows and
don't have a typical fortune file on hand.

>> testfile.seek(randline)
>> print testfile.readline()  #read what is likely half a line
>> print testfile.readline()  #read the next whole line

Alan



More information about the Tutor mailing list