shelve,cPickle模組
close() 關閉檔案物件
flush() 重新整理檔案的緩衝區。緩衝區包含等待寫入或檔案中讀取的資訊。“重新整理“就是執行實際的讀取或寫入操作
isatty() 如果檔案物件是tty(終端)裝置,就返回1
read([size]) 從檔案中讀取資料。
readline([size]) 從檔案中讀取一行
readlines([size]) 從檔案中讀取多行
seek(offset[,location]) 使檔案位置移動offset個位元組。如果沒有指定location,檔案位置從檔案起始處移動。如是指定了location,就從指定位置移動。
tell() 返回檔案的當前位置
write(output) 將字串output寫入檔案
writeline(outputlist) 將outputlist中的每個字串寫入檔案
writelines() 寫入多行資料
一.建立順序訪問檔案
寫入檔案
import sys
try:
file=open("client.dat","w")
except IOError,message:
print >> sys.stderr, "File could not be opened",message
sys.exit(1)
print "Enter the accout,name and age."
while 1:
accountline=raw_input("?")
except EOFError:
break
else:
print >>file,accountline
file.close()
讀取資料
file=open("client.dat","r")
except IOError:
print >> sys.stderr, "File could not be opened"
records=file.readlines()
print "Account",ljust(10)
print "Name",ljust(10)
print "age",ljust(10)
for record in records:
fields=record.split()
print fields[0].ljust(10),
print fields[1].ljust(10),
print fields[2].ljust(10),
shelve模組應用
模擬隨機訪問檔案:
import shelve
coutcredit=shelve.open("credit.dat")
print >> sys.stderr,"File could not be opened"
print "Enter account number (1 to 100,0 to end input)"
accountNumber=int(raw_input("\nEnter account number\n?"))
if 0 < accountNumber <= 100:
print "Enter lastname, Firetname,balance"
currentData=raw_input("?")
outCredit[str{accountNumber)]=currentDdata.split()
elif accountNumber==0:
outCredit.close()
cPickle模組應用
注:cPickle模組的執行效率高於pickle
寫檔案
import cPickle
coutcredit=open("credit.dat","w")
inputlist=[]
except EOFErrot:
inputlist.append(accountline.split())
cPickle.dump(inputlist,outCredit)
讀出資料
outcredit=open("credit.dat","r")
records=cPickle.load(outcredit)
outcredit.close()
print record[0].ljust(15),
print record[1].ljust(10),
print record[2].ljust(20)
shelve,cPickle模組
close() 關閉檔案物件
flush() 重新整理檔案的緩衝區。緩衝區包含等待寫入或檔案中讀取的資訊。“重新整理“就是執行實際的讀取或寫入操作
isatty() 如果檔案物件是tty(終端)裝置,就返回1
read([size]) 從檔案中讀取資料。
readline([size]) 從檔案中讀取一行
readlines([size]) 從檔案中讀取多行
seek(offset[,location]) 使檔案位置移動offset個位元組。如果沒有指定location,檔案位置從檔案起始處移動。如是指定了location,就從指定位置移動。
tell() 返回檔案的當前位置
write(output) 將字串output寫入檔案
writeline(outputlist) 將outputlist中的每個字串寫入檔案
writelines() 寫入多行資料
一.建立順序訪問檔案
寫入檔案
import sys
try:
file=open("client.dat","w")
except IOError,message:
print >> sys.stderr, "File could not be opened",message
sys.exit(1)
print "Enter the accout,name and age."
while 1:
try:
accountline=raw_input("?")
except EOFError:
break
else:
print >>file,accountline
file.close()
讀取資料
import sys
try:
file=open("client.dat","r")
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit(1)
records=file.readlines()
print "Account",ljust(10)
print "Name",ljust(10)
print "age",ljust(10)
for record in records:
fields=record.split()
print fields[0].ljust(10),
print fields[1].ljust(10),
print fields[2].ljust(10),
file.close()
shelve模組應用
模擬隨機訪問檔案:
import sys
import shelve
try:
coutcredit=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)"
while 1:
accountNumber=int(raw_input("\nEnter account number\n?"))
if 0 < accountNumber <= 100:
print "Enter lastname, Firetname,balance"
currentData=raw_input("?")
outCredit[str{accountNumber)]=currentDdata.split()
elif accountNumber==0:
break
outCredit.close()
cPickle模組應用
注:cPickle模組的執行效率高於pickle
寫檔案
import sys
import cPickle
try:
coutcredit=open("credit.dat","w")
except IOError:
print >> sys.stderr,"File could not be opened"
sys.exit(1)
print "Enter account number (1 to 100,0 to end input)"
inputlist=[]
while 1:
try:
accountline=raw_input("?")
except EOFErrot:
break
else:
inputlist.append(accountline.split())
cPickle.dump(inputlist,outCredit)
outCredit.close()
讀出資料
import sys
import cPickle
try:
outcredit=open("credit.dat","r")
except IOError:
print >> sys.stderr,"File could not be opened"
sys.exit(1)
records=cPickle.load(outcredit)
outcredit.close()
for record in records:
print record[0].ljust(15),
print record[1].ljust(10),
print record[2].ljust(20)