Yuck. Talk about a one shot function! Of course it only reads through the file once! You only call the function once. Put a second print randline(f) at the bottom of your script and see what happens :-)
JS > This method only keeps one line in memory, only reads through the file > once, and does not favor lines based on any characteristic of the line. > It's probably fast enough to not even bother keeping an index around: > > #!/bin/env python > > import os > import random > > text = 'shaks12.txt' > if not os.path.exists(text): > os.system('wget http://www.gutenberg.org/dirs/etext94/shaks12.txt') > > f = file(text, 'rb') > > def randline(f): > for i,j in enumerate(f): > if random.randint(0,i) == i: > line = j > return line > > print randline(f) _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor