I am attempting to read a file per line (further down the line I will be splitting each line by comma). My program is reading that there are 10 lines with text in them however when I use the readline() function it returns a blank string.
I have tried to put the readline inside the linecount function, but that failed and I have also tried it with and without the file loop parameter.
QFile = open("questions.txt", "r")
lineCount = 0
fileloop = 0
for line in QFile:
if line != "/n":
lineCount = lineCount + 1
print(lineCount)
while fileloop < lineCount:
print(QFile.readline(fileloop))
fileloop += 1
A sample of my file
What is the name of the rugby compition with Scotland, England, Ierland, Wales, France and Italy?,Six Nations,Sport,1
What’s the maximum score you can achieve in 10-pin bowling?,300,Sport,1
Which horse is the only three-time winner of the Grand National?,Red Rum,Sport,1
Which country won both the men's and women's Six Nations in 2020?,England,Sport,1
QFile.seek(0)between the 2 loops fixed the issuesline != "/n", which will cause your while loop to finish looping over the file early, and the last few lines won't be printed. Is it your intention to print out everything except empty lines?