20190218-学习python使用shelve遇到raise error,…

2019-02-20 00:46:08来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

照书上敲代码,运行了提示raise error, "db type could not be determined",现场如下:

ubuntu-vm:~/code/massageboard$ python
Python 2.7.6 (default, Nov 13 2018, 12:45:42) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shelve
DATA_FILE='guestbook.dat'
>>> DATA_FILE='guestbook.dat'
>>> 
>>> def save_data(name,comment,create_at):
...     database=shelve.open(DATA_FILE)
...     if 'greeting_list' not in database:
...         greeting_list=[]
...     else:
...         greeting_list=database['greeting_list']
...     greeting_list.insert(0,{
...         'name':name,
...         'comment':comment,
...         'create_at':create_at,
...     })
...     database['greeting_list']=greeting_list
...     database.close()
... 
>>> import datetime
>>> save_data('test','test comment',datetime.datetime(2019,02,18,10,0,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in save_data
  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
>>> 

故障排除过程:

ubuntu-vm:~/code/massageboard$ ls
guestbook.dat  guestbook.py  guestbook.pyc  index.html  main.css  __pycache__
ubuntu-vm:~/code/massageboard$ rm guestbook.dat     #删除数据存储文件
ubuntu-vm:~/code/massageboard$ ls
guestbook.py  guestbook.pyc  index.html  main.css  __pycache__


ubuntu-vm:~/code/massageboard$ python Python 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import datetime >>> from guestbook import save_data >>> save_data('test','test comment',datetime.datetime(2019,02,18,10,0,0)) >>>

 


原文链接:https://www.cnblogs.com/coffeebox/p/10395455.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:#3 不使用循环输出1到100

下一篇:Python 反射