[Tutor] How best to determine if a db exists before trying to open it? [Was: try and file existence]

Steven D'Aprano steve at pearwood.info
Sun Aug 16 11:46:29 CEST 2015

On Sat, Aug 15, 2015 at 06:24:12PM -0500, boB Stepp wrote:

> db = sqlite3.connect("my_db.db")
> 1) This will open the db if it exists already, which is normally what
> I will want. But...
> 2) My understanding is that if for whatever reason the db file is not
> found, then the connect statement will create a new instance of the
> db, which is what I normally would not want (Except at the time of
> initial creation).
> If I am understanding everything so far, I think that my situation
> would be appropriate for using os.path.exists().  Is this correct?

Sure, why not?

If might not be an utterly bullet-proof solution, but it will do the 
job for now and you can always revisit it later if needed.


More information about the Tutor mailing list