37,741
社区成员
发帖
与我相关
我的任务
分享#!/usr/bin/python
#Filename:addrbook.py
import sys
import time
import cPickle as p
class contact:
def __init__(self,name,phone):
self.name=name
self.phone=phone
list = [self.name,self.phone]
listdicts = {}
listdicts[name] = phone
cont = 'contactlist.data'
f = file(cont,'a')
p.dump(listdicts,f) #dump the objs to file
f.close()
listdicts.clear()
f = file(cont)
print 'checked in %s: ' % p.load(f) #load contactlistfile
def InputInfo():
running = True
while running:
name = raw_input('Enter name:')
phone = raw_input('Enter Phone Number:')
contact(name,phone)
print '''name is: %s and phone is: %s''' % (name,phone)
forsure = raw_input('''Are You Sure?(enter y/n/q):''')
if forsure == 'y':
running = False
contact(name,phone)
sys.exit()
elif forsure == 'q':
break
else:
continue
def SearchInfo():
name = raw_input('Enter name for search:')
cont = file('contactlist.data')
listdicts = p.load(cont)
cont.close()
print listdicts
print 'you want search is %s' % name
if name in listdicts:
print listdicts[name]
else:
print 'not found the name'
sys.exit()
# program 1st check point
if len(sys.argv) < 2:
print 'must specified a command like --add'
print 'sys.argv is %d' % len(sys.argv)
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'add':
InputInfo()
if option == 'search':
SearchInfo()
else:
print 'no more option,quit job'
sys.exit()