C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Break: When the line equals an empty string, we break out of our while-True loop. We are done reading the file.
Continue: When we encounter a newline string, an empty line was read—but the file has not ended yet. We continue.
Python program that uses readline
# Open the file.
f = open(r"C:\programs\info.txt", "r")
while(True):
# Read a line.
line = f.readline()
# When readline returns an empty string, the file is fully read.
if line == "":
print("::DONE::")
break
# When a newline is returned, the line is empty.
if line == "\n":
print("::EMPTY LINE::")
continue
# Print other lines.
stripped = line.strip()
print("::LINE::")
print(stripped)
Contents: info.txt
Secret insider trading details
ABC
Lottery ticket numbers
1234
Voting manipulation tips
*123
Output
::LINE::
Secret insider trading details
::LINE::
ABC
::EMPTY LINE::
::LINE::
Lottery ticket numbers
::LINE::
1234
::EMPTY LINE::
::LINE::
Voting manipulation tips
::LINE::
*123
::DONE::
Quote: 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.
Input and Output: Python.org