0
import random

line_count = 0
for line in file:
    line_count = line_count + 1

line_count = line_count - 1
print(line_count)
randomLine = random.randrange(0, line_count)
print(randomLine)

lines = file.readlines()
print(lines[randomLine])

I checked what is needed with trying to cancel the index error code, I reduced the line value by 1 so it wont try and print a non existent line. I do not know what is wrong with the code.

This is the input file

Line 16 - 5x - 5 = 20
Line 17 - 5
Line 18 - 1x + 5 = 10
Line 19 - 5

Error Message

Traceback (most recent call last):
  File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 252, in <module>
    mainscreen()
  File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 242, in mainscreen
    main_menu()
  File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 210, in main_menu
    quiz1()
  File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 158, in quiz1
    quiz2()
  File "c:\Users\Liq04\Desktop\SDD Python\python_project_enhanced_v2.py", line 121, in quiz2
    print(lines[randomLine])
IndexError: list index out of range
1
  • Please add the error stack! Commented Mar 2, 2021 at 9:33

2 Answers 2

2

The problem is the after the for loop below completes, the file pointer reaches to the end of the file and so file.readlines() return an empty list.

for line in file:

You can reset the file pointer to start reading from the start again using seek() method:

file.seek(0)

The complete code would be:

file = open('Odata.txt', 'r')
line_count = 0
for line in file:
    line_count = line_count + 1

line_count = line_count - 1
print(line_count)

import random
randomLine = None
randomLine = random.randrange(0, line_count)
print(randomLine)

file.seek(0)
lines = file.readlines()
print(lines[randomLine])

file.close()

Update: As rightly pointed by @SpaceBurger, you don't need most of the above stuff and can simply get the lines count using len(file.readlines()).

Here is the updated code:

import random
with open('Odata.txt', 'r') as file:
    lines = file.readlines()
    line_count = len(lines)
    randomLine = random.randrange(0, line_count)
    print(lines[randomLine])

Opening the file using the context manager with will automagically close the file when it comes out of the with block.

Sign up to request clarification or add additional context in comments.

4 Comments

One could also store the content of lines = file.readlines() then count the number of lines with len(lines). This would remove the need to use file.seek(0) because we can simply use the lines variable.
this will not work after we have iterated over the loop since file.readlines() would return an empty list. I assume you meant to save it before the loop.
Yes, I was too late to edit my comment. Using len(lines) would also remove the need for the 4 or 5 lines of code dedicated to counting the number of lines.
I agree most of it is not required and can be done in one line as you suggested.
0

the problem is you're moving the file pointer to the end in while counting lines write this code before reading line

# file.seek(0) moves the pointer to the starting position

import random

file=open("./test.text","r")
line_count = 0
for line in file:
    line_count = line_count + 1

line_count = line_count - 1
print("line count",line_count)
randomLine = random.randrange(0, line_count)
print("random number",randomLine)
file.seek(0)
print(file.read())

Comments

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.