[Tutor] Reading and Writing (No Arithmetic?)

tbrauch@tbrauch.com tbrauch@tbrauch.com
Fri, 18 Jan 2002 19:33:24 -0500


I am trying to write a quick little program to help me with a menial task...
Isn't that the whole purpose of programming?  I have a small html, let's
call it info.html (because that is its name) file that consists of something
like:

<HTML><BODY>Some Inspirational Quote Here</Body><HTML>

I also have a data file called info.data chock full o' inspirational quotes,
one per line (they also might have some additional HTML such as links).
 What I am trying to do is find the best why to pull a line at random and
insert it into the info.html file.  What works with only a few lines in
the data file is:
[Untested code below, I think it is right]

import random
f = open('info.data', 'r')
i = open('info.html', 'w')
quotes = f.readlines()
newQuote = quotes[random.randrange(0,quotes.len())]
i.write('<HTML><BODY>'+newQuote+'</BODY></HTML>')
f.close()
i.close()

But, how could I get a random line if there are thousand of lines in the
data file without bogging down the computer?

 - Tim