我正在 python 2.7.6 中使用 shelve 进行缓存计算,并且遇到了所描述的问题HERE https://stackoverflow.com/q/35771816/4038337对于我制作并实施的搁置文件建议的解决方案 https://stackoverflow.com/a/35782184/4038337在合并文件的函数中other
进入文件target
:
... # target and other are strings
# Loads the gdbm module, as suggested
mod = __import__("gdbm")
# Open target for modifications
tar = shelve.Shelf(mod.open(target, 'c', 0666)) # THROWS EXCEPTION
# Open other for reading
oth = shelve.Shelf(mod.open(other, 'r'))
...
这两个文件是归我所有,记录在本地文件系统并将 posix 权限设置为0666
,或者等价地,-rw-rw-rw-
在我的 Linux Mint 盒子里,这样已经进行了明显的检查:
$ ls -l
-rw-rw-rw- 1 myusr mygrp 11694080 Sep 17 21:24 cache
-rw-rw-rw- 1 myusr mygrp 12189696 Sep 17 21:23 cache.0
Here, cache
is the target
, and cache.0
is the other
文件。当前工作目录归我所有并具有权限0775
,我可以使用以下命令创建文件touch
, cp
等等,随意,没有问题,我什至设置了我的umask
to 0000
,因此创建新文件0666
,或者等价地,-rw-rw-rw-
权限。
我什至根据其实际文件权限与 gdbm.open() 调用中的权限进行了匹配文档 https://docs.python.org/2.7/library/gdbm.html#gdbm.open;然而,无济于事。
Update:通过运行 python 代码sudo
,即具有超级用户权限时,错误发生在同一条线;但是有不同的消息:gdbm error: Bad magic number
!这很奇怪,因为使用(看似)较低级别的模块(gdbm
相对于shelve
)正是绕过了数据库类型检测。
更新#2: 跑步python whichdb.py
关于文件返回dbhash
;但是,将模块更改为dbhash
加载代码仍然给出以下错误:
bsddb.db.DBAccessError: (13, 'Permission denied')
当以用户身份运行时,但是
bsddb.db.DBInvalidArgError: (22, 'Invalid argument -- BDB0210 ././merge-cache.py: metadata page checksum error')
当使用 sudo 运行时;merge-cache.py
是我的代码的名称。
讨论了这个新错误here https://stackoverflow.com/q/15958917/4038337,与 python 版本有关,但是(i)我的 python 版本与该帖子中的版本不同,并且(ii)创建文件并稍后使用samepython 的版本。
这个答案 https://stackoverflow.com/a/15577654/4038337表示shelve
对于大型数据库来说会被“浪费”,但我报告的问题也发生在较小的数据库上。
问题:如何使用 python-2.7.6 打开这些搁置文件? (升级 python 不是一个选项)。