Search code examples
pythonshelve

shelve does not sync to disk before close


After changing the version of python from 3.5 to 3.7, shelve does no longer write to disk when calling the sync method. However, when I use python 3.5 it does.

I also noticed the on python 3.7 a 'db' extension is appended to the shelve database file.

Does anybody know the reason for this and are there any workarounds?


Solution

  • Turns out that I was missing the python3.7-gdbm package (It's an Ubuntu Xenial Server).

    It can be installed with apt-get:

    sudo apt-get install python3.7-gdbm
    

    After installing the gdbm package python will start storing the shelve as a GNU dbm instead of a Berkeley DB:

    $ file *
    shelve_obj_berkley:            Berkeley DB (Hash, version 9, native byte-order)
    shelve_obj_gdbm:               GNU dbm 1.x or ndbm database, little endian, old