No need to close file?

Gerard Flanagan grflanagan at yahoo.co.uk
Wed Jul 19 04:28:54 EDT 2006


mensanator at aol.com wrote:
> T wrote:
> > Do I need to close the file in this case?  Why or why not?
> >
> > for line in file('foo', 'r'):
> >   print line
>
> I was running a program in IDLE that opened a file for
> reading and forgot to add the close.
>
> The program ran and terminated normally.
>
> But when I tried to open it from Windows Explorer,
> I got the message that it was still in use. Had to close
> IDLE to release it. That wouldn't have happened if I had
> closed it from within the program.

yes, this invariably happens me (with PythonWin) if I try to get away
without a 'finally'

Gerard




More information about the Python-list mailing list