文件读取
#!/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
坚持,专注