CSDN学习社区 python脚本之过滤关键字

python脚本之过滤关键字

12345678910111213141516171819202122232425262728293031323334353637383940414243444546#!/usr/bin/envpython#coding:utf8...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
#coding:utf8
 
 
import os
import sys
import fileinput
import glob
 
 
path = '.'
 
egrep_file = sys.argv[1]
 
 
def grep(egrep_file):
    index = []
    if os.path.exists(egrep_file):
        with open(egrep_file) as f:
            lines = f.readlines()
            for in lines:
                = i.strip()
                if len(s) > 0:
                    index.append(s)
    return index
 
 
ret = grep(egrep_file)
file = r'zhaopin*'
files = glob.glob(file)   #获取所有匹配路径
print (files)
 
#print (ret)
for in files:
    if os.path.isfile(i):                #判断路径是否为文件
        with open(i) as f:
            lines = f.readlines()
            for line in lines:
                for grep_s in ret:
                    if grep_s in line:
                        print i,
        #               print line,
      #  s = line.replace(grep_s,'\033[32;%s\033[0m'%grep_s')
         = line.replace(grep_s,'\033[1;31;40m;%s\033[0m'%grep_s)
 
         print (s)


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐

  • 浏览量 1166
  • 收藏 0
  • 0

所有评论(0)

查看更多评论 
已为社区贡献3条内容