Chinaunix首页 | 论坛 | 博客
  • 博客访问: 123833
  • 博文数量: 31
  • 博客积分: 691
  • 博客等级: 中士
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-16 16:45
文章分类

全部博文(31)

文章存档

2012年(4)

2011年(27)

分类: LINUX

2011-11-15 22:21:58


  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:
阅读(3169) | 评论(0) | 转发(1) |
0

上一篇:Shell脚本调试技术

下一篇:python文件操作

给主人留下些什么吧!~~