shelve文件的读写问题
时间:2011-03-04
来源:互联网
本帖最后由 eeeee159963 于 2011-03-04 11:07 编辑
复制代码
第九行outCredit = shelve.open( "credit.dat" )是读取文件的意思(默认"r"模式),但这个程序的功能是写文件,
我以为第九行应该换成outCredit = shelve.open( "credit.dat" , "w"),不知道对不对呢
- # Fig. 14.9: fig14_09.py
- # Writing to shelve file.
-
- import sys
- import shelve
-
- # open shelve file
- try:
- outCredit = shelve.open( "credit.dat" )
- except IOError:
- print >> sys.stderr, "File could not be opened"
- sys.exit( 1 )
-
- print "Enter account number (1 to 100, 0 to end input)"
-
- # get account information
- while 1:
-
- # get account information
- accountNumber = int( raw_input(
- "\nEnter account number\n? " ) )
-
- if 0 < accountNumber <= 100:
-
- print "Enter lastname, firstname, balance"
- currentData = raw_input( "? " )
-
- outCredit[ str( accountNumber ) ] = currentData.split()
-
- elif accountNumber == 0:
- break
-
- outCredit.close() # close shelve file
我以为第九行应该换成outCredit = shelve.open( "credit.dat" , "w"),不知道对不对呢
作者: eeeee159963 发布时间: 2011-03-04
本帖最后由 iamkey9 于 2011-03-04 15:35 编辑
默认是c - 读写建
shelve.open(filename[, flag='c'[, protocol=None[, writeback=False]]])¶
Open a persistent dictionary. The filename specified is the base filename for the underlying database. As a side-effect, an extension may be added to the filename and more than one file may be created. By default, the underlying database file is opened for reading and writing. The optional flag parameter has the same interpretation as the flag parameter of anydbm.open().
anydbm.open(filename[, flag[, mode]])¶
Open the database file filename and return a corresponding object.
If the database file already exists, the whichdb module is used to determine its type and the appropriate module is used; if it does not exist, the first module listed above that can be imported is used.
The optional flag argument must be one of these values:
Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing
默认是c - 读写建
shelve.open(filename[, flag='c'[, protocol=None[, writeback=False]]])¶
Open a persistent dictionary. The filename specified is the base filename for the underlying database. As a side-effect, an extension may be added to the filename and more than one file may be created. By default, the underlying database file is opened for reading and writing. The optional flag parameter has the same interpretation as the flag parameter of anydbm.open().
anydbm.open(filename[, flag[, mode]])¶
Open the database file filename and return a corresponding object.
If the database file already exists, the whichdb module is used to determine its type and the appropriate module is used; if it does not exist, the first module listed above that can be imported is used.
The optional flag argument must be one of these values:
Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing
作者: iamkey9 发布时间: 2011-03-04
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28