[Tutor] Simple Question...

R. Alan Monroe amonroe at columbus.rr.com
Sat Oct 16 22:52:16 CEST 2004


>         This will work perfectly if your file is small enough to fit in your
> computer's memory. If you want a function that does this on large 
> files, you'll have to use something in those lines:


> import random

> def randomLineFromBigFile(fileName, numLines):
>         whatLine = random.randint(1, numLines)  # choose a random line number
>         source = open(fileName, 'r')
>         i = 0
>         for line in source:
>                 i += 1
>                 if i == whatLine: return line
>         return None


>         This function uses very little (and a constant amount of) memory. The 
> downside is that you have to know the total number of lines in the file 
> (that's the numLines argument) before calling it. It's not a very hard 
> thing to do.

Wouldn't it be much quicker to do something like this?

import os.path
import random

size = os.path.getsize('test.txt')
print size

randline = random.randint(1, size)
print randline

testfile = open('test.txt', 'r')
testfile.seek(randline)
print testfile.readline()  #read what is likely half a line
print testfile.readline()  #read the next whole line
testfile.close()

You'd just need to add some exception handling in the event you tried
to read off the end of the file.

Alan



More information about the Tutor mailing list