python中的货架模块无法正常工作:“无法确定db类型" [英] Shelve module in python not working: "db type cannot be determined"

查看:210
本文介绍了python中的货架模块无法正常工作:“无法确定db类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Python创建一个简单的密码存储程序,而且看起来很简单,所以我想知道我是否使用了错误的存储方式.

I am trying to make a simple password-storing program in Python, and it seems pretty simple so I am wondering if I am using shelve wrong.

我有主要的.py文件:

I have the main .py file:

import shelve

passwords = shelve.open('./passwords_dict.py')

choice = raw_input("Add password (a) or choose site (c)?")

if choice[0] == 'a':
    site_key = raw_input("Add for which site? ").lower()
    userpass = raw_input("Add any info such as username, email, or passwords: ")

    passwords[site_key] = userpass

else:
    site = raw_input("Which site? ").lower()
    if site in passwords:
        print "Info for " + site + ": " + passwords[site]
    else:
        print site, "doesn't seem to exist!"

print "Done!"

passwords.close()

另一个文件passwords_dict.py只是一个空字典.

And the other file, passwords_dict.py, is just an empty dictionary.

但是当我尝试运行程序时,出现此错误:

But when I try to run the program, I get this error:

Traceback (most recent call last):
File "passwords.py", line 3, in <module>
passwords = shelve.open('passwords_dict.py')
File "/usr/lib/python2.7/shelve.py", line 239, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/usr/lib/python2.7/shelve.py", line 223, in __init__
Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
File "/usr/lib/python2.7/anydbm.py", line 82, in open
raise error, "db type could not be determined"
anydbm.error: db type could not be determined

当我尝试使用anydbm时,出现此错误:

When I try to use anydbm instead, I get this error:

Traceback (most recent call last):
File "passwords.py", line 3, in <module>
passwords = anydbm.open('passwords_dict.py')
File "/usr/lib/python2.7/anydbm.py", line 82, in open
raise error, "db type could not be determined"
anydbm.error: db type could not be determined

当我尝试使用dbm时,出现此错误:

And when I try to use dbm instead, I get this error:

Traceback (most recent call last):
File "passwords.py", line 3, in <module>
passwords = dbm.open('./passwords_dict.py')
dbm.error: (2, 'No such file or directory')

我做错了什么?

推荐答案

我认为您误解了货架模块的工作方式.它打开一个数据库文件.当您尝试打开包含Python脚本的现有文件时,它会尝试检测该文件包含的数据库类型(因为书架支持多个后端数据库).

I think you're misunderstanding how the shelve module works. It opens a database file. When you try and open an existing file that contains a Python script, it's trying to detect what database type the file contains (since shelve supports multiple backend databases).

我认为相反,您想要这样的东西:

I think instead you want something like this:

import os
import shelve

curdir = os.path.dirname(__file__)
passwords = shelve.open(os.path.join(curdir, 'password_db'))

这将在与脚本password_db.<db>相同的目录中创建一个新文件,其中<db>是特定于实现的数据库文件扩展名.

This will create a new file in the same directory as your script called password_db.<db> where <db> is an implementation-specific database file extension.

这篇关于python中的货架模块无法正常工作:“无法确定db类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆