title: How should I read a file line-by-line in Python?
body: <p>In pre-historic times (Python 1.4) we did:</p>
  <pre><code>fp = open('filename.txt')
  while 1:
      line = fp.readline()
      if not line:
          break
      print(line)
  </code></pre>
  <p>after Python 2.1, we did:</p>
  <pre><code>for line in open('filename.txt').xreadlines():
      print(line)
  </code></pre>
  <p>before we got the convenient iterator protocol in Python 2.3, and could do:</p>
  <pre><code>for line in open('filename.txt'):
      print(line)
  </code></pre>
  <p>I've seen some examples using the more verbose:</p>
  <pre><code>with open('filename.txt') as fp:
      for line in fp:
          print(line)
  </code></pre>
  <p>is this the preferred method going forwards?</p>
  <p>[edit] I get that the with statement ensures closing of the file... but why isn't that included in the iterator protocol for file objects?</p>