title: Memory-efficent way to iterate over part of a large file
body: <p>I normally avoid reading files like this:</p>
  
  <pre><code>with open(file) as f:
      list_of_lines = f.readlines()
  </code></pre>
  
  <p>and use this type of code instead.  </p>
  
  <pre><code>f = open(file)
  for line in file:
       #do something
  </code></pre>
  
  <p>Unless I only have to iterate over a few lines in a file (and I know which lines those are) then it think it is easier to take slices of the list_of_lines.  Now this has come back to bite me.  I have a HUGE file (reading it into memory is not possible) but I don't need to iterate over all of the lines just a few of them. I have code completed that finds where my first line is and finds how many lines after that I need to edit.  I just don't have nay idea how to write this loop. </p>
  
  <pre><code>n = #grep for number of lines 
  start = #pattern match the start line 
  f=open('big_file')
  #some loop over f from start o start + n
        #edit lines  
  </code></pre>
  
  <p>EDIT: my title may have lead to a debate rather than an answer. </p>