4

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'
2
  • Could it be the spaces in the filename? try rename the file from 7.18.2016 PCR etrakit.accdb to file.accdb and update the script too and try again? Commented Nov 15, 2016 at 17:36
  • copy all the MS Access database files into CSV format... You do realize a relational database is not the same as a flat file text file? You should be more specific in exporting all tables/queries in MS Access files to csv format. Commented Nov 15, 2016 at 20:15

2 Answers 2

1

[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.

Sign up to request clarification or add additional context in comments.

8 Comments

@Learner The backslash character means that the next character has special meaning in a string. So deciphering the double backslash... The first backslash tells python to prepare for a special string character, the second backslash is the special string character (which in this case is a backslash)
Yeah I tried the above suggestion you recommended and even edited the original post to reflect the change but it still throws the same error.
@Learner check this
Yeah that's one of the posts I used to sketch together the script.
conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\T:\\DataDump\\7.18.2016 PCR etrakit.accdb")
|
0

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

3 Comments

Yeah thanks Aaron, there was a typo there. The C root should not be there. Now if you don't mind how can I see the files I have in that Access database.
So, to clarify, the above solution worked for you? Can you specify what worked? The first or second solution (with escaped spaces)? Or was it simply removing the C:\ portion of the file path? As for accessing the files, that is grounds for a second topic given that you are now connecting successfully.
Yeah needed to remove the C: root folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.