Re: Opening files without closing them
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Opening files without closing them

From: 3c273 <nospam@nospam.com>
Date: Wed Mar 08 2006 - 19:48:02 CET

"Peter Hansen" <peter@engcorp.com> wrote in message
news:mailman.2850.1141759783.27775.python-list@python.org...
>
> So this is better, though probably excessive in small scripts:
>
> try:
> f = open('file')
> except IOError:
> # do something else
> else:
> try:
> content = f.read()
> finally:
> f.close()
>
> This takes advantage of "else" on try statements, which executes only if
> the except statement is not executed.
>
Thank you for your reply. I had forgotten that you could use 'else' in a
'try' statement. I like this solution. Thanks again.
Louis
Received on Sun Apr 30 11:12:54 2006