Well he could implement the indexing into his program and check mtimes to 
decide if he needs to reindex.

But yes, as long the file fits into memory, readlines (or 
list(file("quotes,txt")) makes more sense.

Andreas

-- Ursprüngl. Mitteil. --
Betreff:        Re: [Tutor] reading random line from a file
Von:    "Alan Gauld" <[EMAIL PROTECTED]>
Datum:          15.07.2007 06:39


"max baseman" <[EMAIL PROTECTED]> wrote


> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly

You can either get theclines to be the same length and
use a random index to seek() to the start of the line you want.
Or you can build a separate index file which records where
each line starts and randomly select one of thiose. But that
requires that the quotes file is only changed programmatically
so that the index file can be rebuilt each time you add/delete
a quote. (Or you build an indexing program)

Its much easier (unless the file is huge) to just use readlines()

HTH

Alan G.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to