losing handles of open files

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Jun 1 18:30:54 EDT 2006


Please don't post non-text message bodies to discussion
forums. Message bodies should be plain text.

"Anthra Norell" <anthra.norell at tiscalinet.ch> writes:

> If a piece of code exits with an exception before it closes an open
> file, that file seems to remain locked, which is real pain in the
> butt

You will want to examine the 'finally' clause, which is executed after
the 'try' suite regardless of exceptions.

    <URL:http://docs.python.org/ref/exceptions.html>
    <URL:http://docs.python.org/ref/try.html#try>

You may also be interested in the 'with' statement, coming in Python
2.5, which will be a more natural way of expressing this idiom.

    <URL:http://docs.python.org/dev/whatsnew/section-generators.html>

-- 
 \         "If nature has made any one thing less susceptible than all |
  `\    others of exclusive property, it is the action of the thinking |
_o__)                       power called an idea"  -- Thomas Jefferson |
Ben Finney




More information about the Python-list mailing list