文件读取

#!/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

 

转载于:https://www.cnblogs.com/houzhiqing/p/4754788.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值