0

so im looking for the correct variable that will end the while loop once the loop reaches the end of the file. Here is my code so far:

file_name = input("Please enter file name: ")

open_file = open(file_name,"r")

while ##variable## != "":
    line1 = open_file.readline()
    line2 = open_file.readline()
    line3 = open_file.readline()
    line4 = open_file.readline()

Lets say that my file that i opened looks like this:

Babbage, Charles
10.0   9.5    8.0  10.0
 9.5  10.0    9.0
85.0  92.0   81.0
Turing, Alan
10.0   8.0    6.0
10.0  10.0    9.0   9.5
90.0  92.0   88.5
Hopper, Grace
10.0  10.0    8.0
10.0  10.0    9.0   9.5
90.0  92.0   88.0

I need the loop to end as soon as it reads the last line, heres some pseudocode:

#Get first data
#while data is not sentinel
    #process the data
    #
    #
    #
    #
    #Get next data

Hopefully thats clear, i appreciate your help.

2
  • You can get the line count with this function: stackoverflow.com/questions/845058/… Commented Oct 31, 2017 at 18:50
  • Unfortunately it will be an arbitrary number of lines. Commented Oct 31, 2017 at 18:55

3 Answers 3

1

Use a for loop instead:

for line in open_file:
    #do stuff
    print(line)

From the Tutorial - Reading and Writing Files


If you are forced to use a while loop:
readline will return an empty string when it reaches the end of the file so you can make the sentinel an empty string.

sentinel = ''

Before the while loop read a line

line = open_file.readline()

Use line in the while loop condition

while line != sentinel:

At the bottom of the loop, read another line.

Even though there may be empty lines in the file, those lines should still contain an end-of-line character like "\n" so it won't be an empty string.

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

1 Comment

Unfortunately this needs to be a while loop per instruction of my professor. Is there a way to do this with a while loop and a sentinel ?
0

As stated above, a for loop is the simplest and best way to read from a file. Unlike Java or C, where you could easily craft a condition for a while loop that remains true only while there is still content left to be read from the file, it is more awkward to do so in Python.

Files in Python expose an iterator, thus you can iterate over them using a for loop (most natural). You could also use the built-in next() method, as such:

f = open('abc.txt','r')
while(True):
    try:
        line = next(f)
        print(line,end='')
    except:
        break

1 Comment

This kind of makes sense but shouldnt it have a sentinel somewhere, like if #variable# = "": break
0

Of course this isn't optimal but to use a while loop use the Function I have in the comments:

def file_len(fname):
with open(fname) as f:
    for i, l in enumerate(f):
        pass
return i + 1



file_name = input("Please enter file name: ")

numOfLines = file_len(file_name)
open_file = open(file_name,"r")

count = 0
while count != numOfLines:
    #enter code here
    count += 1

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.