- 总觉用bash在一些事上有些力不从心,很大程度上是自己水平有限阿。这是一个学习python原因。曾经看过有人这么说过:学习一门编程语言不是说你一定能把它用得多么得炉火纯青,至少那能给你对一个问题的一种不同思考方式。
- #!/usr/bin/python
-
-
# A simple phone book. Use the struct of dictionary
-
-
import pickle
-
import os
-
-
-
Datastring = {}
-
-
# open the data file.
-
if os.path.exists('data.txt'):
-
data = open('data.txt','r+') # 'r+' open a file but don't clear it.
-
else:
-
data = open('data.txt','w+') # 'w+' open a file and clear it. 'r+' and 'w+' both open
-
# a file with Write and read.
-
string = data.read()
-
if len(string) == 0:
-
print 'The file is empty'
-
else:
-
data.seek(0) # change the location of the file.
-
Datastring = pickle.load(data)
-
data.seek(0)
-
data.write('')
-
data.flush()
-
-
# The main menu.
-
print ' '*16,'*'*35
-
print """
-
What do you want to do?
-
-
1. Join a new name.
-
2. Find a man.
-
3. Modify a record.
-
4. Delete one.
-
5. Show all.
-
6. exit."""
-
-
# The function Join.
-
-
def Join():
-
print 'Input the name: '
-
name = raw_input()
-
while True:
-
print 'Input the phone: '
-
phone = raw_input()
-
if phone.isdigit():
-
break
-
Datastring[name] = phone
-
-
# The function Find()
-
-
def Find():
-
print 'Input the name you want to search:'
-
name = raw_input()
-
if name in Datastring:
-
print '''
-
Find:
-
Name:%s
-
Phone:%s'''%(name,Datastring[name])
-
else:
-
print 'Not found.'
-
-
# The function Show().
-
-
def Show():
-
print '*'*60
-
print 'All people:'
-
D_sort = sorted(Datastring.iteritems(),key=lambda d:d[0])
-
for p in range(len(D_sort)):
-
print 'Name:%s\t\tPhone:%s'%(D_sort[p][0],D_sort[p][1])
-
print 'Over.'
-
print '*'*60
-
-
# The function Modify().
-
-
def Modify():
-
print 'Input the name that you want to modify:'
-
name = raw_input()
-
if name in Datastring:
-
while True:
-
print 'Input the phone:'
-
phone = raw_input()
-
if phone.isdigit():
-
Datastring[name] = phone
-
break
-
else:
-
print 'Wrong name'
-
-
# The function Delete().
-
-
def Delete():
-
print 'Input the name that you want to delete.'
-
name = raw_input()
-
if name in Datastring:
-
Datastring.pop(name)
-
print 'Done it.'
-
else:
-
print 'Not found.'
-
-
-
# The main block.
-
while True:
- print "You're choice: