Python学习(关于一个简单的通讯录)

3157阅读 0评论2011-11-15 lys5300
分类:LINUX


  1.    总觉用bash在一些事上有些力不从心,很大程度上是自己水平有限阿。这是一个学习python原因。曾经看过有人这么说过:学习一门编程语言不是说你一定能把它用得多么得炉火纯青,至少那能给你对一个问题的一种不同思考方式。


  2. #!/usr/bin/python

  3. # A simple phone book. Use the struct of dictionary

  4. import pickle
  5. import os


  6. Datastring = {}

  7. # open the data file.
  8. if os.path.exists('data.txt'):
  9.     data = open('data.txt','r+')            # 'r+' open a file but don't clear it.
  10. else:
  11.     data = open('data.txt','w+')            # 'w+' open a file and clear it. 'r+' and 'w+' both open
  12.                                             # a file with Write and read.
  13. string = data.read()
  14. if len(string) == 0:
  15.     print 'The file is empty'
  16. else:
  17.     data.seek(0)        # change the location of the file.
  18.     Datastring = pickle.load(data)
  19.     data.seek(0)
  20.     data.write('')
  21.     data.flush()

  22. # The main menu.
  23. print ' '*16,'*'*35
  24. print """
  25.             What do you want to do?

  26.             1. Join a new name.
  27.             2. Find a man.
  28.             3. Modify a record.
  29.             4. Delete one.
  30.             5. Show all.
  31.             6. exit."""

  32. # The function Join.

  33. def Join():
  34.     print 'Input the name: '
  35.     name = raw_input()
  36.     while True:
  37.         print 'Input the phone: '
  38.         phone = raw_input()
  39.         if phone.isdigit():
  40.             break
  41.     Datastring[name] = phone

  42. # The function Find()

  43. def Find():
  44.     print 'Input the name you want to search:'
  45.     name = raw_input()
  46.     if name in Datastring:
  47.         print '''
  48.             Find:
  49.             Name:%s
  50.             Phone:%s'''%(name,Datastring[name])
  51.     else:
  52.         print 'Not found.'

  53. # The function Show().

  54. def Show():
  55.     print '*'*60
  56.     print 'All people:'
  57.     D_sort = sorted(Datastring.iteritems(),key=lambda d:d[0])
  58.     for p in range(len(D_sort)):
  59.         print 'Name:%s\t\tPhone:%s'%(D_sort[p][0],D_sort[p][1])
  60.     print 'Over.'
  61.     print '*'*60

  62. # The function Modify().

  63. def Modify():
  64.     print 'Input the name that you want to modify:'
  65.     name = raw_input()
  66.     if name in Datastring:
  67.         while True:
  68.             print 'Input the phone:'
  69.             phone = raw_input()
  70.             if phone.isdigit():
  71.                 Datastring[name] = phone
  72.                 break
  73.     else:
  74.         print 'Wrong name'

  75. # The function Delete().

  76. def Delete():
  77.     print 'Input the name that you want to delete.'
  78.     name = raw_input()
  79.     if name in Datastring:
  80.         Datastring.pop(name)
  81.         print 'Done it.'
  82.     else:
  83.         print 'Not found.'


  84. # The main block.
  85. while True:    
  86.     print "You're choice:
上一篇:Shell脚本调试技术
下一篇:python文件操作