Why do I have to close the shell for my script to work? (writing to text files)
<p>I have the following code in a .py file:  </p>

<pre><code>f = open('exampleTextFile.txt', 'w')
f.write('This is a sentence.')
f.close
</code></pre>

<p>I open it up and press F5 to run.</p>

<p>The IDLE shell opens up and runs the code without a hitch.  But then when I find the text file in my directory and open it up the text isn't there.  Then when I close the shell and reopen the text file the text now appears.  Why do I need to close the shell and what code can I write so that I don't have to do this?</p>

<p>I know the answer must be simple! Thanks.</p>

<p>Your file is not closed properly. You forgot the parenthesis:</p>

<pre><code>f.close()
</code></pre>

python python-2.7