0

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.

agentp
  • 6,849
  • 2
  • 19
  • 37
shantanuo
  • 31,689
  • 78
  • 245
  • 403

2 Answers2

2

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()
Timothy Brown
  • 2,220
  • 18
  • 22
  • 2
    Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)') – shantanuo Nov 06 '14 at 16:56
  • @shantanuo A quick search here found this [solution](http://stackoverflow.com/questions/3251702/how-to-connect-pyodbc-to-an-access-mdb-database-file). – Timothy Brown Nov 06 '14 at 21:22
1

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'
Nikos Tavoularis
  • 2,843
  • 1
  • 30
  • 27
DataSSA
  • 11
  • 4