1

我正在使用一个生成序列化 Python 架子的软件包。

在生成货架的远程机器上,我可以打开并完美处理它们。但是,当我将它们复制到本地计算机上时,它们将无法再打开。

我将问题追溯到 dbm 子模块(https://docs.python.org/3.1/library/dbm.html)。在远程,当在搁置(格式:data.db)上投射 dbm.whichdb() 时,输出为 'dbm.ndbm',因此似乎安装了 ndbm,我认为可能是第三方 Oracle Berkeley改为使用 DB,我从 dbm 库的init .py 文件中的源代码中读取它(因为数据格式是 .db 而不是 .pag,.dir):

def whichdb(filename):
    """Guess which db package to use to open a db file.

    Return values:

    - None if the database file can't be read;
    - empty string if the file can be read but can't be recognized
    - the name of the dbm submodule (e.g. "ndbm" or "gnu") if recognized.

    Importing the given module may still fail, and opening the
    database using that module may still fail.
    """

    # Check for ndbm first -- this has a .pag and a .dir file
    try:
        f = io.open(filename + ".pag", "rb")
        f.close()
        f = io.open(filename + ".dir", "rb")
        f.close()
        return "dbm.ndbm"
    except OSError:
        # some dbm emulations based on Berkeley DB generate a .db file
        # some do not, but they should be caught by the bsd checks
        try:
            f = io.open(filename + ".db", "rb")
            f.close()
            # guarantee we can actually open the file using dbm
            # kind of overkill, but since we are dealing with emulations
            # it seems like a prudent step
            if ndbm is not None:
                d = ndbm.open(filename)
                d.close()
                return "dbm.ndbm"
        except OSError:
            pass
...

在我的本地机器上,运行相同的代码会生成三个文件:data.bak、data.dat 和 data.dir。对它们调用 dbm.whichdb() 会产生“dbm.dumb”。对从远程复制的文件强制转换 dbm.whichdb() 会产生“无”,这意味着根据文档,数据库不可读或已损坏。

我怀疑我缺少打开这些数据库的东西。

在 dbm 库中,dumb.py 文件充满了内容,但是,ndbm.py 只说

"""Provide the _dbm module as a dbm submodule."""

from _dbm import *

我认为应该有其他东西可以使用 ndbm 子模块。

如何打开这些 ndbm / Berkeley DB 数据库?

4

0 回答 0