71

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
Prog1020
  • 4,530
  • 8
  • 31
  • 65

5 Answers5

165

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.

lennon310
  • 12,503
  • 11
  • 43
  • 61
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    This prints two line-separators instead of one. How can I make the output exactly the same as the input? – slartidan Feb 10 '19 at 14:36
  • @AshwiniChaudhary is it possible to loop back. i.e. decrement the file pointer. I have not found a working version of that yet. – Alexander Cska Mar 18 '19 at 18:41
  • @slartidan : in python3, `print(line, end='')` gets you the output the same as the input. – aizquier Nov 20 '20 at 21:11
  • thank you for the `for line in f` bit! I always forget that and it's hard to find in the python docs – Mike B May 13 '21 at 17:41
15

Just iterate over each line in the file. Python automatically checks for the End of file and closes the file for you (using the with syntax).

with open('fileName', 'r') as f:
    for line in f:
       if 'str' in line:
           break
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
8

There are situations where you can't use the (quite convincing) with... for... structure. In that case, do the following:

line = self.fo.readline()
if len(line) != 0:
     if 'str' in line:
         break

This will work because the the readline() leaves a trailing newline character, where as EOF is just an empty string.

Damian Vogel
  • 1,050
  • 1
  • 13
  • 19
2

You can stop the 2-line separation in the output by using

    with open('t.ini') as f:
       for line in f:
           print line.strip()
           if 'str' in line:
              break
Chris
  • 21
  • 1
  • This removes all leading and trailing whitespace, which may not be desired. You can specify line-ending characters to only remove those: `line.strip("\n\r")`. – Pulseczar Oct 06 '22 at 17:58
1

The simplest way to read a file one line at a time is this:

for line in open('fileName'):
    if 'str' in line:
        break

No need for a with-statement or explicit close. Notice no variable 'f' that refers to the file. In this case python assigns the result of the open() to a hidden, temporary variable. When the for loop ends (no matter how -- end-of-file, break or exception), the temporary variable goes out of scope and is deleted; its destructor will then close the file.

This works as long as you don't need to explicitly access the file in the loop, i.e., no need for seek, flush, or similar. Should also note that this relies on python using a reference counting garbage collector, which deletes an object as soon as its reference count goes to zero.