Q:

What does this block of Python code do?

I am writing a python hangman program, and I wanted to be able to randomly generate a word from a file, and it works. But I got one line of this code off a website, and it helps me to do what I need to do, but I dont know how.
Thanks
   offset = random.randint(0, os.stat(filename)[6]) # ?????
   fd = file(filename, 'rb')
   fd.seek(offset)
   fd.readline()
   return fd.readline()

A:

os.stat(filename)[6] simply returns the size, in bytes, of the file named by filename. You can read more about os.stat() in the documentation.
random.randint(...) generates a random integer between zero and n, where n is the size of the file obtained via os.stat().
The code then seeks to that (random) position in the file. The chances are that this position is in the middle of a line. Therefore, the code reads the partial line and discards it. It then reads the next line and returns it.
Finally, the code has a bug: if the random position lands on the last line of the file, the second readline() will have nothing to read.
edit: Also, as noted by @Russell Borogove in the comments, this method doesn't ensure that lines are chosen with equal probability.

