(Feb-08-2020, 04:43 AM)Nomex Wrote: How would it print forever? If f has come to a line which is empty wouldnt it stop?No it would not stop,it always read whole file to the end using while or for loop.
A better way,so can iterate over file object line bye line(do not need
readline()
).with open('cities.txt') as f: for line in f: print(line.strip())Example how stop(break out of loop) if there is a empty line.
with open('cities.txt') as f: for line in f: if line in ('', '\n'): break print(line.strip())