Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:50:22+00:00 2026-05-26T09:50:22+00:00

I am writing a python hangman program, and I wanted to be able to

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:50:23+00:00Added an answer on May 26, 2026 at 9:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a python script that parses a 3D model file from one
I'm writing a Python program with a lot of file access. It's running surprisingly
I'm writing python program to build mac-address cache using pcap. But pcap module for
I'm using Eclipse for writing Python, and I want to be able to easily
Writing a python program, and I came up with this error while using the
I'm writing a Python backup script and I need to find the oldest file
I'm writing Python scripts for Blender for a project, but I'm pretty new to
I've seen some people writing Python code by creating one class and then an
each unit test I'm running is writing python code out to a file, then
I've seen this question about advice for C# programmers writing Python code but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.