简明python教程最后作业,联系人记录小程序,版本0.2,共6个文件,执行最后一个即可
# -*- coding: UTF-8 -*-
'''
Created on 2012-6-26
@author: raven-win7
'''
#filename:fileopen.py
import cPickle as d
class fileopen:
def __init__(self,filename):
pl={}
self.filename=filename
# print self.filename
try:
f=file(self.filename)
except:
print 'create new file'
f=file(self.filename,'w')
d.dump(pl,f)
f.close()
else:
if len(f.readline())==0:
d.dump(pl,f)
f.close()
else:
f.close()
# -*- coding: UTF-8 -*-
'''
Created on 2012-6-26@author: raven-win7
'''
#filename:watchlist.py
from fileopen import *
class watchlist(fileopen):
def __init__(self,filename):
fileopen.__init__(self, filename)
f=file(self.filename)
fall=d.load(f)
for name in fall:
print 'name is:%s address is:%s'%(name,fall[name])
f.close
# -*- coding: UTF-8 -*-
'''
Created on 2012-6-26@author: raven-win7
'''
#filename:insterp.py
from fileopen import *
class insterp(fileopen):
def __init__(self,filename):
fileopen.__init__(self, filename)
print 'input name:\n',
name=raw_input()
print 'input address:\n',
address=raw_input()
f=file(self.filename)
fadd=d.load(f)
fadd[name]=address
f=file(self.filename,'w')
d.dump(fadd,f)
f.close()
print 'save success'
# -*- coding: UTF-8 -*-
'''
Created on 2012-6-26@author: raven-win7
'''
#filename:delep.py
from fileopen import *
class delep(fileopen):
def __init__(self,filename):
fileopen.__init__(self, filename)
print 'input the name you want to delete:\n',
name=raw_input()
f=file(self.filename)
fdel=d.load(f)
f.close()
if name in fdel:
del fdel[name]
f=file(self.filename,'w')
d.dump(fdel,f)
f.close()
else:
print 'delete success'
# -*- coding: UTF-8 -*-
'''
Created on 2012-6-26@author: raven-win7
'''
from fileopen import *
class scp(fileopen):
def __init__(self,filename):
fileopen.__init__(self, filename)
print 'input the name you want to select:\n',
name=raw_input()
f=file(self.filename)
fsc=d.load(f)
if name in fsc:
print 'name is:%s \naddress is:%s'%(name,fsc[name])
else:
print 'not find'
f.close()
# -*- coding: UTF-8 -*-
'''
Created on 2012-6-26@author: raven-win7
'''
#filename:
#from personlist import *
from delep import *
from insterp import *
from scp import *
from watchlist import *
import sys
filename='test0627.data'
while True:
inum=raw_input('input your number: 0:watch all 1:inster new person 2:delete one 3:select one 4:close')
if inum in "0":
yourcommd=watchlist(filename)
elif inum in "1":
yourcommd=insterp(filename)
elif inum in "2":
yourcommd=delep(filename)
elif inum in "3":
yourcommd=scp(filename)
elif inum in "4":
print 'close the program'
break
else:
'your number is not in 1 or 2 or 3 or 4'
sys.exit()