shantanuo
shantanuo

Reputation: 32346

open access file in python

I am not able to open the access file using python. I am not sure if the problem is with the mdb file or the python commands.

In [1]: import sys, subprocess

In [2]: DATABASE = 'Exam_BackUp.mdb'

In [3]: table_names = subprocess.Popen(["mdb-tables", "-1", DATABASE], stdout=subprocess.PIPE).communicate()[0]
Couldn't open database.

How do I know if the file is microsoft access file? I have checked that mdbtools is installed on my Ubuntu server.

I need to open the (access or fortran) file and save the contents to csv.

Upvotes: 0

Views: 8679

Answers (2)

DataSSA
DataSSA

Reputation: 11

You can convert it by the Terminal using mdbtool like this:

Install mdbtools and upgrade it:

pip install mdbtools
pip install --upgrade pip

Then look for the name of the table inside the mdb file:

home/Docs$ mdb-tables 'file.mdb'

And finally convert the file to .csv with this line:

home/Docs$ mdb-export 'file.mdb' 'name_of_table' > 'file.csv'

Upvotes: 1

Timothy Brown
Timothy Brown

Reputation: 2280

Why not try opening it with an ODBC driver?

A good example is here, reproducing it for your case would be along the lines of:

import pyodbc

DBfile = 'Exam_BackUp.mdb'
conn = pyodbc.connect('FILEDSN='+DBfile)
cursor = conn.cursor()

# Do whatever you want with SQL selects, etc

cursor.close()
conn.close()

Upvotes: 2

Related Questions