Yes. "with" runs the "__exit__()" method of whatever you're creating there, 
passing in the exception info if it's ending because of an exception. For 
standard files the __exit__ behavior is to close down the file either way.

https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers

For the sqlite3 module connection object: on a bad exit from an exception it 
will do a rollback, on a clean exit it will do a commit, and run .close() 
either way.

with sqlite3.connect(dbFile) as conn:
   blah blah
#no need to call .close here

https://docs.python.org/2/library/sqlite3.html#using-the-connection-as-a-context-manager

-----Original Message-----
From: sqlite-users [mailto:sqlite-users-boun...@mailinglists.sqlite.org] On 
Behalf Of Simon Slavin
Sent: Thursday, April 06, 2017 5:27 PM
To: SQLite mailing list
Subject: Re: [sqlite] Testing sqlite db to see if exists & ready


On 6 Apr 2017, at 10:07pm, David Raymond <david.raym...@tomtom.com> wrote:

> with open(fi, "r") as f:

In Python, once you fall outside the scope of "with open()" does it 
automatically close the file for you ?  If so, that’s pretty neat.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to