with statements and exceptions

Paul Rubin http
Wed Oct 25 15:47:28 EDT 2006


John Salerno <johnjsal at NOSPAMgmail.com> writes:
>      def create_sql_script(self):
>          with open('labtables.sql') as sql_script:
>              return sql_script.read()
> 
> Does the file still get closed even though I have a return statement
> inside the with block?

Yes, I think so.  I'm not running 2.5 yet but this is supposed to be
the idea of the with statement.  As for the open failing, you'd deal
with that using try/except around the whole thing.

http://www.python.org/dev/peps/pep-0343/ explains the with statement
in detail.  



More information about the Python-list mailing list