Anonymous file closing
Duncan Booth
me at privacy.net
Fri Jun 11 05:15:40 EDT 2004
Sergey Krushinsky <lunarium at comail.ru> wrote in
news:mailman.845.1086940793.6949.python-list at python.org:
> Hello all,
>
> If I need to read a string from a file and use syntax like:
> text = open(filename, 'r').read()
> ...
> is the file object ever closed?
>
> With best regards,
> Sergey
>
>
Yes it is closed, but it isn't well defined as to exactly when it is
closed. You can safely assume that the file will be closed sometime between
the read returning and your program exiting.
If you aren't worried about portability, and you are using the C
implementation of Python then you may find it is closed immediately but it
is poor practice to depend on it.
More information about the Python-list
mailing list