pythoncmacosmagic-numbersgdbm

Can a GDBM database created in C be opened in Python?


I have a Python program in which I am trying to use this rhyming dictionary to look up rhymes.

Part of the dictionary library setup works by a C program creating three gdbm .db files. The code that does this is publicly available here (from 'get the source' section), and the key part of the C gdbm builder compile.c looks like this:

#include <gdbm.h>

...<snip>...

int main(int argc, char *argv[]) {
  FILE *input;
  GDBM_FILE output;

...<snip>...

output = gdbm_open(argv[2], 0, GDBM_NEWDB, 0644, 0);

...<snip>...

gdbm_sync(output);

...<snip>...

words.db is created, and it can be successfully accessed from the bundled command line interface.

According to the library's example page, I should be able to use the python gdbm module to access the database.

However, I cannot. When I run the following:

import gdbm
words = gdbm.open('/usr/share/rhyme/words.db')

I get the following error:

    words = gdbm.open('/usr/share/rhyme/words.db')
gdbm.error: Bad magic number

Is there a file incompatibility problem? Do some gdbm files created in C not open with Python?

(This is with Python 2.7 on OSX 10.6.8. Python's gdbm was installed via MacPorts)


Solution

  • How did you create the words.db file? It looks like the words.db is corrupted or in a format not supported by your specific version of the gdbm library on Mac OS X.