文件读取

#!/bin/python
import sys
f = file('mingdan.txt','w')
f.write('''1   lijuan  bj
2   thouzhiqing IT
3   zhangwu  IT
4   user4   4
5   user5   5
6   user6   6
3   user3   3
7   user7   7
8   user8   8
9   user9   9''')
f.close()
while True:
    match_yes = 0
    f = file('mingdan.txt')
    select_input = raw_input('\033[32;1mplease input your select:\033[0m').strip()
    if len(select_input) == 0:
        continue
    if select_input == 'Q' or select_input == 'q':
        sys.exit()
    while True:
        line = f.readline()
        if len(line) ==0 :break
        if select_input in line:
            print  '\033[32;1m%s\033[0m' %line
            match_yes += 1
        else:
            pass
    if match_yes == 0: print '\033[31;1mnot found\033[0m'
    else:
        print '\033[32;1mhave %s lines\033[0m' %match_yes

效果

[root@localhost ~]# python select.py 
please input your select:2
2   thouzhiqing IT

have 1 lines
please input your select:3
3   zhangwu  IT

3   user3   3

have 2 lines
please input your select:q

 

posted on 2015-08-24 15:01  侯志清  阅读(168)  评论(0编辑  收藏  举报

导航