[Tutor] Threads.... Unhandle my Exception you Feind!

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 12 Jun 2001 09:09:39 -0700 (PDT)


On Tue, 12 Jun 2001 kromag@nsacom.net wrote:

> db=engine.OpenDatabase("\windows\desktop\terror.mdb")

Here's the bug!  When you put a backslash in a string, Python treats it as
a special "escape" character.  We can see how Python interpreted this
particular string:

###
>>> print "\windows\desktop\terror.mdb"
\windows\desktop	error.mdb
###

It didn't find equivalent escape codes for '\w' or '\d', but '\t' matches
with the 'tab' escape code.  Assumedly, if engine.OpenDatabase can't find
a file, it returns None, and that explains the error you're getting:

###
AttributeError: 'None' object has no attribute 'Execute'
###


The fix is to tell Python to treat the backslashes as regular
characters.  There are two main ways to do this: the first is to double up
all the backslashes:

    db = engine.OpenDatabase("\\windows\\desktop\\terror.mdb")

because the escape code for '\\' is the single backslash character.  The
other way is to use "raw strings", which are just strings which don't put
any special meaning to the backslash:

    db = engine.OpenDatabase(r"\windows\desktop\terror.mdb")

The leading 'r' in front tells Python to treat this particular string as
a raw string.

Hope this helps!