I have a problem connecting to a local Access file that has accented characters in both the path and file name. I am new to Python, so this is what I have managed so far:
# coding=utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import pyodbc
scriptDir = os.path.dirname(os.path.realpath(__file__)).decode('mbcs')
dbRelPath = "MøreCase_v2.accdb"
dbAbsPath = scriptDir + '\\' + dbRelPath
print (os.path.exists(dbAbsPath))
dbConStr = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + dbAbsPath
print (dbConStr)
cnxn = pyodbc.connect(dbConStr)
The first print returns 'True', the other one prints the full file name, but the connection fails with the following error:
Exception UnicodeEncodeError: UnicodeEncodeError('ascii', u'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\xxx\case\M\xf8re\M\xf8reCase_v2.accdb', 121, 122, 'ordinal not in range(128)') in ignored
I tried to decode the connection string back to the system encoding
cnxn = pyodbc.connect(dbConStr.encode('mbcs'))
but then I get the following error instead:
Traceback (most recent call last): File "mwe.py", line 11, in cnxn = pyodbc.connect(dbConStr.encode('mbcs')) UnicodeDecodeError: 'ascii' codec can't decode byte 0xf8 in position 121: ordina l not in range(128)
I tried also 'cp1252' and 'utf-8', but it gives the same error (only the character code is different in utf-8).
More info: the script file is saved as utf-8. I am on 64-bit English Windows 7 with Norwegian 'locales'.
Thanks in advance.