[Tutor] read a random line from a file

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 16 Dec 2001 23:52:21 -0800 (PST)


On Mon, 17 Dec 2001, Andrei Kulakov wrote:

> On Sun, Dec 16, 2001 at 11:20:29PM -0500, Kirk Bailey wrote:
> > OK, I know there is a simple way to do it, and I cannot locate the
> > correct ommand. I want to open a flat text file, read ONE randomly
> > selected line into a string, and close the file. I need the command for
> > reading a line at random, and what module it is in. I understand there
> > is such a command already defined, and it is simple to use, but canot
> > find it again.


Actually, I'm not sure if it's built in at all.  However, we can cook up a
few definitions so that it becomes a tool we can use.


To get a random line from a file, it might be useful to write something
that tells us how many lines a file contains.  Here's one way to do it:

###
def countLines(file):
    """Given a file, returns the number of lines it contains.

    The current file position should be preserved as long as the file
    supports tell() and seek()."""
    old_position = file.tell()
    count = 0
    while file.readline() != '':
        count = count + 1
    file.seek(old_position)
    return count
###


If we had a way to choose a random number between 0 and the countLines()
of a file, we'd be in business.  Thankfully, there is something in the
library that can help us: random.randrange():


###
>>> random.randrange(10)
4
>>> random.randrange(10)
7
>>> random.randrange(10)
4
###


There is documentation on random.randrange() here:

    http://www.python.org/doc/lib/module-random.html

if you're interested in playing around with random stuff.  *grin*


Also, it would be great if, when we knew which line we wanted to pull out,
we could get get that particular line.  There's a module called
'linecache' that can do this for us:

    http://www.python.org/doc/lib/module-linecache.html

But if 'linecache' wasn't there, it still wouldn't be too hard to cook
something up like it.


With these tools, it becomes possible to write a simple random line
grabber:

###
def getRandomLine(filename):
    """Given a filename, returns a random line."""
    linecount = countLines(open(filename))
    chosen_line_number = random.randrange(linecount)
    return linecache.getline(filename, chosen_line_number)
###



Writing function definitions can be a lot of fun.  Hope this helps!