python writing to a file
<p>I keep getting this "writing to a closed file error" while trying to compile the following code:</p>

<pre><code>fout = open('markov_output.txt', 'w')  

for i in range( MAXGEN ) :
            # get our hands on the list
    key = (w1,w2)
    sufList = table[key]
            # choose a suffix from the list
    suf = random.choice( sufList )

    if suf == NONWORD :     # caught our "end story" marker.  Get out
            if len( line ) &gt; 0 :
                    fout.write(line)
            break
    if len( line ) + len( suf ) &gt; MAX_LINE_LEN :
            fout.write(line)
            line = ""
    line = line + " " + suf

    w1, w2 = w2, suf
    fout.close()
</code></pre>
