Use readline to read txt file python3
a.readline()
will return ''
an empty string when no more data is available, you need to check that and then break your while
, eg:
while True: line = a.readline() if not line: break
If it's not purely for learning purposes then you really should be using a with
statement and for-loop to process the file, line by line:
with open('SampleTxt.txt') as fin: for line in fin: pass # do something
It's more clear as to your intent, and by using the with
block, the fileobj will be released on an exception or when the block ends.