[Tutor] reading random line from a file

Luke Paireepinart rabidpoobear at gmail.com
Sun Jul 15 06:06:54 CEST 2007


max baseman 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
>
> for line in text.readlines():
> 	lines.append(text)
>   
You don't have to read the lines in this way.
Just do lines = text.readlines() directly.

There's no way that you can just directly read a specific line without 
reading in the rest of the file,
because Python doesn't know beforehand where newlines are located inside 
of the file.
So even if this were possible, it would still read in all of the file up 
to and including the line you want, so that it could
count the number of newlines.

Why is it a problem to input it all at once?
-Luke


More information about the Tutor mailing list