关于dbm.error 的问题,求解,编程小白

from datetime import datetime

import shelve,os

class userdatabase(object):

def init(self,dbfile):

self.db = {}

if os.path.exists(dbfile):

self.db = shelve.open(dbfile, 'c')

self.dbfile = dbfile

self.flag = False

def __del__(self):  
    data = shelve.open(self.dbfile, 'c')    
    data.update(self.db)    
    data.close()  

def login(self,user,pwd):  
    if user not in self.db:  
        self.flag = False  
    elif self.db[user][0] == pwd:  
        self.db[user][1] = datetime.now()  
        self.flag = True  

def deluser(self,user):  
    if self.flag:  
        self.db.pop(user)  
    else:  
        print ('login first')  

def updateuser(self,user,pwd):  
    if self.flag:  
        self.db[user] = [pwd,datetime.now()]  
    else:  
        print ('login first')  

def listall(self):  
    if self.flag:  
        for user in self.db:  
            print( user, self.db[user][0], self.db[user][1])
    else:  
        print( 'login first')


a = userdatabase('userdata.txt')
Traceback (most recent call last):
File "", line 1, in
a = userdatabase('userdata.txt')
File "C:\Users\Administrator\Desktop\用户注册.py", line 16, in init
self.db = shelve.open(dbfile, 'c')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shelve.py", line 243, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shelve.py", line 227, in init
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\dbm__init__.py", line 88, in open
raise error0
dbm.error: db type could not be determined

1个回答

a = userdatabase('userdata.txt')
去掉扩展名.txt试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问