python脚本运行错误原因,求解!

运行python脚本出现如下错误:脚本如下:#!/usr/bin/envpythonimportsys,shelvedefstore_person(db):#"""#Qu... 运行python脚本出现如下错误:
脚本如下:
#!/usr/bin/env python
import sys,shelve
def store_person(db):
# """
# Query user for data and store it in the shelf object
# """
pid = raw_input('Enter unique ID number: ')
person = {}
person['name'] = raw_input('Enter name: ')
person['age'] = raw_input('Enter age: ')
person['phone'] = raw_input('Enter phone number: ')
db[pid] = person
def lookup_person(db):
# """
# Query user for ID and desired field,and fetch the corresponding data from
# the Shelf object
# """
pid = raw_input('Enter ID number: ')
field = raw_input('What would you like to know? (name,age,phone) ')
field = field.strip().lower()
print field.capitalize() + ':', \
db[pid][field]
def print_help():
print 'The available commands are:'
print 'store : Stores information about a person'
print 'lookup : Looks up aperson from ID number'
print 'quit : Save changes and exit'
print '? : Prints this message'
def enter_command():
cmd = raw_input('Ener command (? for help): ')
cmd = cmd.strip().lower()
return cmd
def main():
database = shelve.open('/home/luo/python/test.txt')
try:
while True:
cmd = enter_command()
if cmd == 'store':
store_person(database)
elif cmd == 'lookup':
lookup_person(database)
elif cmd == '?':
print_help()
elif cmd == 'quit':
return
finally:
database.close()
if __name__ == '__main__': main()
运行后错误如下:
Traceback (most recent call last):
File "database1.py", line 48, in ?
if __name__ == '__main__': main()
File "database1.py", line 34, in main
database = shelve.open('/home/luo/python/test.txt')
File "/usr/lib64/python2.4/shelve.py", line 231, in open
return DbfilenameShelf(filename, flag, protocol, writeback, binary)
File "/usr/lib64/python2.4/shelve.py", line 212, in __init__
Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback, binary)
File "/usr/lib64/python2.4/anydbm.py", line 80, in open
raise error, "db type could not be determined"
anydbm.error: db type could not be determined
Exception exceptions.AttributeError: "DbfilenameShelf instance has no attribute 'writeback'" in ignored
展开
 我来答
百度网友619cd53fa
2011-12-18 · TA获得超过440个赞
知道小有建树答主
回答量:201
采纳率:0%
帮助的人:260万
展开全部
shelve是一个对象持久化保存方法,将对象保存到文件里面,一般来说对象的保存和恢复都是通过shelve来进行的。
你的问题是test.txt已经存在,并且格式与shelve不符,所以提示 "db type could not be determined"
解决方法: 删除/home/luo/python/test.txt文件,首次运行后会自动生成该文件。
另外,缺省方式数据文件是二进制的,最好不要用txt结尾来误导别人。

下面是运行结果

[zx@zx-linux test]$ ./test.py
Ener command (? for help): store
Enter unique ID number: 1
Enter name: 2
Enter age: 3
Enter phone number: 4
Ener command (? for help): quit
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
狠狠爱lj
2011-12-18 · TA获得超过5809个赞
知道小有建树答主
回答量:1345
采纳率:88%
帮助的人:534万
展开全部
test.txt 需要是一个 persistent dictionary。我也不太懂,但是能不能看一下你的test.txt?

参考资料: http://docs.python.org/library/shelve.html

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式