0

I've been testing reading lines from a text file and finally got my program to execute correctly. But I'm not sure why the while loop exits execution.

filepath = 'LastQuizPlan.txt'  
with open(filepath) as fp:  
    line = fp.readline()
    count = 1
    while line: 
        print(line.strip)
        line=fp.readline()
        count+=1

I found the code online from http://stackabuse.com/read-a-file-line-by-line-in-python/ and manipulated it slightly, it works fine but i need to create a write up for it as well and dont know how the while line: works exactly? it works exactly as i want it to work but i dont understand what its expression is.

I've been using the following statement for trying to figure this out

while expression:
   statement(s)

but still just dont understand the expression? why does it automatically know that when count is out of the range of lines in "LastQuizPlan.txt", to stop the loop.

also here is the contents of "LastQuizPlan.txt" Unit 1 Electrical stuff 12/06/17

Scott Baldric
  • 465
  • 3
  • 10

2 Answers2

0

So if we swing over to the documentation for Python for readline we find the following:

f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline.

And if we go over to another piece of the documentation about truthy-ness we find:

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

None

False

zero of any numeric type, for example, 0, 0L, 0.0, 0j.

any empty sequence, for example, '', (), [].

Therefore, with these two items combined we have the answer to the problem.

readline keeps returning non-empty strings while there are lines to be read. When it reaches the EOF, it returns an empty string. When the while loop checks the following condition: while '', it treats the empty string '' as a False value and ends execution of the loop.

You can see this doesn't execute anything with the quick Python statement:

while '':
    print("Hello")

When executed, this will print nothing because the while body never executes.

Scott Baldric
  • 465
  • 3
  • 10
  • thank you very much, i though that was how the expression worked but wanted to check i was right before adding this to my write-up – PotOTea Feb 12 '18 at 11:44
  • Awesome. I'm glad I could help. :) If you found that this answer was helpful and fully answered your problem you should also mark it as accepted: https://stackoverflow.com/help/someone-answers – Scott Baldric Feb 12 '18 at 11:50
0

From the Python docs:

if f.readline() returns an empty string, the end of the file has been reached

Since empty strings are evaluated as false in Python, the while condition is no longer true when EOF is reached.

npas
  • 163
  • 2
  • 7