简体   繁体   中英

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()

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.

To expand upon aix's answer, after we have a random integer within the "range" of the file, we go to that location with fd.seek(offset) . We use fd.readline() to drop the line we are on, and move to the next one. Then we use fd.readline() to return the entire current line we are on.

Note that if you end up on the last line of the file, you will return an empty string. To demonstrate set your offset to os.stat(filename)[6] - 1 and use readline twice.

I tried to add this as a comment, but couldn't include a code example.

Here's the code you included with the last line/first line bug fixed:

size = os.stat(filename)[6]
offset = random.randint(0, size) # ?????
fd = file(filename, 'rb')
fd.seek(offset)
fd.readline()
if fd.tell() == size:
    fd.seek(0)
return fd.readline()

It doesn't fix the uniformity problem as described by @russell-borogove.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM