shelve,cPickle模å
close() å
³éæ件对象
flush() å·æ°æ件çç¼å²åºãç¼å²åºå
å«çå¾
åå
¥ææ件ä¸è¯»åçä¿¡æ¯ãâå·æ°âå°±æ¯æ§è¡å®é
ç读åæåå
¥æä½
isatty() å¦ææ件对象æ¯tty(ç»ç«¯ï¼è®¾å¤ï¼å°±è¿å1
read([size]) ä»æ件ä¸è¯»åæ°æ®ã
readline([size]) ä»æ件ä¸è¯»åä¸è¡
readlines([size]) ä»æ件ä¸è¯»åå¤è¡
seek(offset[,location]) 使æ件ä½ç½®ç§»å¨offset个åèãå¦æ没ææå®location,æ件ä½ç½®ä»æ件起å§å¤ç§»å¨ãå¦æ¯æå®äºlocation,å°±ä»æå®ä½ç½®ç§»å¨ã
tell() è¿åæ件çå½åä½ç½®
truncate([size]) å é¤æ件ä¸çæ°æ®ãå¦æ没ææå®size,å°±å é¤æææ°æ®ï¼å¦ææå®äºsize,å°±æå¤åªå é¤æå®çåèæ°ã
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)
温馨提示:答案为网友推荐,仅供参考