Learner Learner - 18 days ago 6
Python Question

Python ODBC connection: not a valid file name error

I am new to Python and have been assigned the task to copy all the MS Access database files(we have five) into CSV format using Python. I have searched through lots of posts on Stack Overflow and sketched together this amateur snippet. I need to see the files I have in my MS Access database. Can someone please provide assistance.

Pyodbc Error - Python to MS Access

open access file in python

import pyodbc

conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=T:\\DataDump\\7.18.2016 PCR etrakit.accdb")

conn = pyodbc.connect(conn_string)

cursor = conn.cursor()

cursor.close()
conn.close()

print 'All done for now'

Answer

Per this post:

Try doing it as a single line

conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};C:\\T:\\DataDump\\7.18.2016 PCR etrakit.accdb;'

However, I am a bit confused by your file path. On the root of your C:\ drive, you have a directory named T:?

It may also be worth noting that file paths with spaces in the name are not always handled as expected. An alternate approach would be to try and escape the spaces in your file path:

conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};C:\\T:\\DataDump\\7.18.2016\ PCR\ etrakit.accdb;'
Comments