初学python,刚刚看完byte of python,结尾的地方有个实例是要创建自己的命令行地址簿程序,花了一天时间写了一个,环境是python 2.6,使用ulipad编写。程序中没有创建类来表示一个人的信息,只有简单的名字和地址信息格式,实现添加,删除联系人和显示地址簿信息的功能,且也没有进行异常处理。暂且如此,留待以后完善。
代码如下:
#! /usr/bin/env python
#coding=utf-8
'''an example: Addressbook command
'''
import cPickle as p
import shutil
import os
abfile='ab.data'
abTempfile='abTemp.data'
def funPrintOption():
print '1. print adressbook'
print '2. add to adressbook'
print '3. del from adressbook'
print '4. exit'
def funPrintAddress():
f=file(abfile)
#ab=p.load(f)
lines = f.readlines()
for x in lines:
print x,
f.close()
def funStr2Dict(b):
c = {}
c[b.split(':')[0]] = b.split(':')[1]
return c
def funAddToAddress(**keys):
print "keys type=%s" % type(keys)
print "keys=%s" % str(keys)
shutil.copyfile(abfile, abTempfile)
fTmp = file(abTempfile,"r")
lines = fTmp.readlines()
for name,address in keys.items():
bSkip = False
for x in lines:
dictx = funStr2Dict(x.strip('\n'))
if dictx.has_key(name):#如果包含name就跳过
bSkip = True
break
if bSkip == True:
print 'the person %s is in this adressbook' % name
continue
abOne = {name : address}
f=file(abfile,'a')
#f.write(str(abOne)+'\n')
f.write(name+':'+address+'\n')
f.close()
print 'add %s' % name
fTmp.close()
os.remove(abTempfile)
def funDelFromAddress(*keys):
print "keys type=%s" % type(keys)
print "keys=%s" % str(keys)
shutil.copyfile(abfile, abTempfile)
f = file(abfile,"w")
f.write('')
f.close()
fTmp = file(abTempfile,"r")
lines = fTmp.readlines()
i = 0
for x in lines:
dictx = funStr2Dict(x.strip('\n'))
bSkip = False
for name in keys:
if dictx.has_key(name):#如果包含name就跳过
bSkip = True
break
if bSkip == True:
print 'del %s' % name
continue
f = file(abfile,"a")
f.write(x)
f.close()
print 'the person %s is not in line %d' % (name, i)
i = i + 1
fTmp.close()
os.remove(abTempfile)
def funDelFromAddressModify(*keys):
print "keys type=%s" % type(keys)
print "keys=%s" % str(keys)
if os.path.isfile(abTempfile) == False:
fTmp = file(abTempfile, "w")
fTmp.close()
f = file(abfile,"r")
lines = f.readlines()
i = 0
for x in lines:
dictx = funStr2Dict(x.strip('\n'))
bSkip = False
for name in keys:
if dictx.has_key(name):#如果包含name就跳过
bSkip = True
break
if bSkip == True:
print 'del %s' % name
continue
fTmp = file(abTempfile,"a")
fTmp.write(x)
fTmp.close()
print 'the person %s is not in line %d' % (name, i)
i = i + 1
f.close()
shutil.copyfile(abTempfile, abfile)
os.remove(abTempfile)
def main():
funPrintOption()
option = (int)(raw_input('Enter an option : '))
if os.path.isfile(abfile) == False:
f = file(abfile, "w")
f.close()
while option != 4:
if option == 1:
funPrintAddress()
elif option == 2:
name = raw_input('Enter a name : ')
address = raw_input('Enter an address : ')
abToAdd = {name : address}
bToAdd = False
while (name != '') and (address != ''):
if bToAdd == False:
bToAdd = True
abToAdd[name] = address
name = raw_input('Enter a name : ')
address = raw_input('Enter an address : ')
if bToAdd:
funAddToAddress(**abToAdd)
elif option == 3:
name = raw_input('Enter a name : ')
allname = []
if name != '':
allname.append(name)
while True:
name = raw_input('Enter a name : ')
if name != '':
allname.append(name)
else:
break
#funDelFromAddress(allname)
funDelFromAddressModify(*allname)
else:
print "There's not the option"
return
funPrintOption()
option = (int)(raw_input('Enter an option : '))
if __name__ == '__main__':
main()