python命令行地址簿程序

初学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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值