[Tutor] reading random line from a file
Tiger12506
keridee at jayco.net
Tue Jul 17 19:18:31 CEST 2007
> wow thats pretty cool :) it's a bit above my level but it's interesting
> :) thanks
I'm deeply flattered! Thank you.
>> #############################################
>> from os import stat
os.stat returns a tuple whose 7th member is the size of the file. (python
docs)
>> from random import randint
randint(a,b) returns a random integer between a and b, inclusive
>> def getrandomline(f, length):
>> pos = randint(0,length)
Picks a random position between the beginning and the end of the file.
>> f.seek(pos)
Seek to that position in the file - i.e. the next character read will be at
that position in the file.
>> while f.read(1)!='\n':
This sets up the loop. Read a character, if it is a newline then break the
loop
>> try:
>> f.seek(-2,1)
However, if it's not a newline character, that means we are in the middle of
a line, so we move the file position two characters back from where we just
were. Two characters because f.read(1) moves the position forward one. One
step forward, two steps back means read character right before. Continuing
this loop means that eventually we will back up until we meet a newline
character, that is, the beginning of the line where our randomly chosen
character belongs.
>> except IOError: f.seek(0)
This is a special case where randint chose a character in the first line.
Thinking about it a bit, we realize that backing up will never find a
newline, and loop will never break. OOPS! I just realized a mistake I made.
There should be a break afterwards.
except IOError:
f.seek(0)
break
See! Anyway. When you seek before the beginning of a file, an IOError is
raised. I caught it here and set the file position properly. (The beginning
of the first line in this special case)
>> return f.readline()
Since the file position is set at the beginning of a random line, the
readline function will read that line and return it.
>> f = file("quotes.txt","rb")
>> sizeoffile = stat("quotes.txt")[6]
As I said above, the 7th member of the stat tuple gives the file size so
that I can use it in randint
>> while (1):
>> print getrandomline(f, sizeoffile),
Obviously you won't use this - it was just to maintain the program while I
checked it's memory usage.
>> f.close()
>> ###################################
See! Not a bit above your level. ;-)
HTH,
JS
More information about the Tutor
mailing list