1

I have this bit of code that's inside a perpetual while loop, and whenever I try to get a random line from a text file, it occasionally throws an index error. Can anybody help me with this? I've already checked my text file and there are 452 lines inside. At first I thought it was some number error so I reduced the upper bound, but the error keeps occurring. The starred piece of code is what is causing the error.

        if len(bank)==445:
            bank = []

        randinte = random.randint(1,450)
        samecheck(randinte, bank, 450)

        text_file = open("text.txt", "r")
        **line = text_file.readlines()[randinte]**
        twitter.update_status(
            status=line)
        text_file.close()
        bank.append(randinte)

EDIT: Thank you for all the help! This is the code I ended up using and working. repopulate() is a method that populates bank from 1-451 sequentially.

        if len(bank)==5:
            bank = []
            repopulate()

        random.shuffle(bank)   
        text_file = open("text.txt", "r")
        lines = text_file.readlines()
        line = lines[bank.pop()]
        twitter.update_status(
            status=line)
        text_file.close()
3
  • 1
    Why not use len() of the readlines() result to pick a random number? Commented Dec 8, 2013 at 2:05
  • I'm not following, isn't readlines() the method to return the line in a text file? Commented Dec 8, 2013 at 2:07
  • 1
    I mean: lines = text_file.readlines() then randinte = random.randint(len(lines)) and line = lines[randinte]. That way at least line 0 can be picked too. Commented Dec 8, 2013 at 2:09

1 Answer 1

2

Its not clear without seeing your text file why you might be getting these index errors, but its best to avoid hard-coding things like lengths of files if possible anyway. Another option would be to use the choice method in the random module to just choose a random line returned from readlines(), as below:

    if len(bank)==445:
        bank = []

    text_file = open("text.txt", "r")
    lines = text_file.readlines()

    line = random.choice(lines) # choose a random line

    twitter.update_status(
        status=line)
    text_file.close()
Sign up to request clarification or add additional context in comments.

7 Comments

I think the OP is storing randinte in bank to test for repeated choices first. So the specific line number is something the OP is storing here.
That said, in that case, read the file once and use random.shuffle() then lines.pop() each time you want a random line would be a better choice.
Right! If they are in fact doing that, then there's absolutely no reason to be reading in the same file over and over.. My assumption was that this was for a new text file each time.
Would it be possible to get the line # from this method? I would like to append it to bank until len(bank) == 445 edit: yes you are correct in your assumption, but I've read that it's good to close the file after each usage.
Do you need the line number for a particular reason? Why not just have a variable "count" that adds +1 each time a line is added until count == 445? That said, if you do actually want to grab the line number, do it with the index method: linenumber = lines.index(line)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.