title: Read and print from a text file N lines at a time using a generator only
body: <p>Python 3.6.0</p>
  
  <pre><code>textfile = "f:\\mark\\python\\Alice_in_Wonderland.txt"
  
  N = 60
  
  
  def read_in_lines(file, n):
      with open(file) as fh:
          for i in range(n):
              nlines = fh.readline()
              if nlines:
                  yield nlines
              else:
                  break
  
  for lines in read_in_lines(textfile, x):
      print(lines)
  </code></pre>
  
  <p>File is here:  <a href="https://www.gutenberg.org/files/11/11.txt" rel="nofollow noreferrer">https://www.gutenberg.org/files/11/11.txt</a></p>
  
  <p>My goal is to read in this file N lines at a time, then print the lines,
  then read in the next N lines, print, repeat...</p>
  
  <p>If N = 3, output should look like:</p>
  
  <pre><code>line1
  line2
  line3
  
  line4
  line5
  line6
  
  line7
  line8
  line9
  
  line10  &lt;-- assumes this is the last line in the file
  </code></pre>
  
  <p>The above print pattern should hold for any value of 'N'.</p>
  
  <p>If 'N' = 4:</p>
  
  <pre><code>line1
  line2
  line3
  line4
  
  line5
  line6
  line7
  line8
  </code></pre>
  
  <p>etc.  You get the idea.</p>
  
  <p>NO lists.  No built in functions (islice, etc.).</p>
  
  <p>I need to use a generator only.
  Each iteration <em>must</em> contain a string containing up to the
  number of lines specified by 'N'.</p>
  
  <p>Two issues:</p>
  
  <p>1) The above code returns 'N' lines, then stops. I assume I need to put the whole
  thing in a loop, but I am unsure of how to proceed. (Newbie...)</p>
  
  <p>2) The file contains A LOT of blank lines. Every single time I try to use strip()
  or any of it's variants, regardless of how big I make 'N' it only ever prints one line.</p>
  
  <p>nlines = fh.readline().strip  &lt;-- adding in .strip()
  With N = 6000 I get:</p>
  
  <pre><code>Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll
  
  Process finished with exit code 0
  </code></pre>
  
  <p>If I get rid of .strip() I get all the lines but not in the format I want.</p>
  
  <p>I am on a Win 10 machine.  In Notepad++ all of the end of file symbols are CRLF.</p>