user6727547
user6727547

Reputation:

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'

Upvotes: 4

Views: 2558

Answers (2)

Aaron St. Clair
Aaron St. Clair

Reputation: 469

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;'

Upvotes: 0

Shubham R
Shubham R

Reputation: 7644

[UPDATED]Try running this

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

use double backslash instead.

Upvotes: 1

Related Questions