Python: read all text file lines in loop
file, python
Solution
There's no need to check for EOF in python, simply do:
with open('t.ini') as f:
for line in f:
# For Python3, use print(line)
print line
if 'str' in line:
break
Why the `with` statement:
It is good practice to use the `with` keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way.
Problem
I want to read huge text file line by line (and stop if a line with "str" found). How to check, if file-end is reached? ``` fn = 't.log' f = open(fn, 'r') while not _is_eof(f): ## how to check that end is reached? s = f.readline() print s if "str" in s: break ```