[Tutor] How best to determine if a db exists before trying to open it? [Was: try and file existence]
robertvstepp at gmail.com
Sun Aug 16 01:24:12 CEST 2015
On Sat, Aug 15, 2015 at 6:00 PM, Cameron Simpson <cs at zip.com.au> wrote:
> On 15Aug2015 15:20, Clayton Kirkwood <crk at godblessthe.us> wrote:
>>> Behalf Of Laura Creighton
>>> To: boB Stepp <robertvstepp at gmail.com>
>>> In a message of Sat, 15 Aug 2015 14:24:21 -0500, boB Stepp writes:
>>> >I understand your points, but wonder then what is the intended use for
>>> >os.path.exists()? That is, in what types of circumstances would it be
>>> >both appropriate and safe to use?
>>> If you want to locate dangling symlinks, os.path.exists will return
>>> False, so
>>> the symlink is there, but the file it pointed to is long gone.
>> Can't you do that with os.path.open() and get a value in os.path.status?
>> think that is the thing to call)
> Open does more that os.stat (which is what os.path.exists uses underneath).
> There are plenty of times you will want to know a file exists but not have
> permission to open it. Also, open can have side effects if the target file
> is a device or a socket/pipe.
> Always use the smallest thing you can to achieve an effect: stat is smaller
> than open.
I actually had a specific example in mind when I asked my question.
My current project will require me to create and then use an SQLite
db. My concern is that after:
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
I'm just now in the process of reading up on SQLite, SQL, and Python's
DB API. So far I have seen no mention that the connect statement
returns anything if the db file does not already exist.
If I am understanding everything so far, I think that my situation
would be appropriate for using os.path.exists(). Is this correct?
More information about the Tutor