[Tutor] read a random line from a file

Andrei Kulakov ak@silmarill.org
Mon, 17 Dec 2001 01:25:23 -0500

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.

#!/usr/bin/env python2

import os, sys, random

proj = os.path.expanduser("~/.t/p")
f = open(proj)
projects = []
lines = f.readlines()
for l in lines:
    if l.strip():
print random.choice(projects),

It's good if file isn't too large..

> -- 
> Respectfully,
>              -Kirk D Bailey (C)2001
>               Addme! icq #27840081
> end
> Within the sweep of his sword, Each man is an Ubar.
> http://www.howlermonkey.net/
> http://www.sacredelectron.org/
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org