Strange nested loop behavior while handling an exception
<p>Goal: if count is larger than actual line count, in <code>except</code> block: tell user and have them press enter. set <code>count</code> equal to total number of lines in file and retry the loop.   </p>

<pre><code>count = 10000
with open('mobydick_ch1.txt') as f:

        while 1:
            lines = []
            try:
                for i in range(count):
                    lines.append(next(f))  # iterate through file and append each line in range
                    break
            except StopIteration:
                if not input("File does not contain that many lines, press enter to continue printing maximum lines:"):
                    for i, k in enumerate(f, 1):
                        count = i

        f.close()  # close file

        # format output. enumerate lines, start at 1
        # http://stackoverflow.com/questions/4440516/in-python-is-there-an-elegant-
        # way-to-print-a-list-in-a-custom-format-without-ex
    print(''.join('Line {0}: {1}'.format(*k) for k in enumerate(lines, 1)))
</code></pre>

<p>I am currently getting:</p>

<blockquote>
  <p>File does not contain that many lines, press enter to continue printing maximum lines:</p>
</blockquote>

<p>every time I press enter. What is causing this unwanted behavior?</p>

<p>You already exhausted the file, you can't then read from the file <em>again</em> without seeking back to 0. As a result your <code>for i, k in enumerate(f, 1):</code> loop exits immediately. The same then applies to every future iteration of your <code>while 1:</code> loop; the file is still at the end and all access with <code>next()</code> will raise a <code>StopIteration</code> immediately.</p>

<p>You already know how many lines you have read, just set <code>count = len(lines)</code>. There is no need to read the whole file <em>again</em> just to set <code>count</code>.</p>

<p>It'd be better if you used <a href="https://docs.python.org/2/library/itertools.html#itertools.islice" rel="nofollow"><code>itertools.islice()</code></a> to get your 1000 lines:</p>

<pre><code>from itertools import islice

count = 10000
with open('mobydick_ch1.txt') as f:
    lines = list(islice(f, count))  # list of up to count lines
if len(lines) &lt; count:
    input("File does not contain that many lines, press enter to continue printing maximum lines:")
    count = len(lines)  # set count to actual number of lines
</code></pre>

<p>If you are trying to <em>wait</em> until a file contains at least <code>count</code> lines, you'll have to re-open the file each time and seek to the last recorded location:</p>

<pre><code>lines = []
pos = 0
while len(lines) &lt; count:
    with open('mobydick_ch1.txt') as f:
        f.seek(pos)
        lines.extend(islice(f, count - len(lines)))
        pos = f.tell()
    if len(lines) &lt; count:
        input("File does not contain that many lines, press enter to continue printing maximum lines:")
</code></pre>

python file