iterating over file object in Python does not work, but readlines() does but is inefficient
<p>In the following code, if I use: </p>

<pre><code>for line in fin:
</code></pre>

<p>It only executes for 'a'</p>

<p>But if I use:</p>

<pre><code>wordlist = fin.readlines()
for line in wordlist:
</code></pre>

<p>Then it executes for a thru z.</p>

<p>But <code>readlines()</code> reads the whole file at once, which I don't want.</p>

<p>How to avoid this?</p>

<pre><code>def avoids():
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    num_words = {}

    fin = open('words.txt')

    for char in alphabet:
      num_words[char] = 0
      for line in fin:
        not_found = True
        word = line.strip()
        if word.lower().find(char.lower()) != -1:
          num_words[char] += 1
    fin.close()
    return num_words
</code></pre>
