[Tutor] reading random line from a file

John Fouhy john at fouhy.net
Sun Jul 15 06:08:01 CEST 2007


On 15/07/07, max baseman <dos.fool at gmail.com> wrote:
> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly
> i tried
> a=randrange(820)+1
> text.readline(a)
>
> and i would prefer not having to bring evryline into the program then
> picking like

The 'fortune' program in unix/linux produces random quotes from a
quote file.  As I understand it, it builds separate index files for
each input file.  I'm not sure exactly how it works, but I would guess
the index files contain byte offsets for the start and end of each
quote.  So you would read the whole index file (which will be much
shorter than the main quote file), select one at random, then use
.seek() and .read() to read just the bytes you are interested in from
the main file.

-- 
John.


More information about the Tutor mailing list