Skip to content

Instantly share code, notes, and snippets.

@wusisu
Created July 8, 2016 07:41
Show Gist options
  • Save wusisu/e08ee53513c4410cf9ddd1ba5b0b80f5 to your computer and use it in GitHub Desktop.
Save wusisu/e08ee53513c4410cf9ddd1ba5b0b80f5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os, sys
import fnmatch
listonly = False
skipexts = ['.js']
def visitfile(fname,searchkey):
global fcount,vcount,list_of_contain_lines
try:
if not listonly:
if os.path.splitext(fname)[1] in skipexts:
raw = open(fname).read()
if raw.find(searchkey) != -1:
# print '%s has %s '%(fname,searchkey)
lines = raw.split('\n')
for line in lines:
if searchkey in line:
list_of_contain_lines.append(line)
# print line
fcount+=1
except: pass
vcount +=1
def visitor(args,directoryName,filesInDirectory):
for fname in filesInDirectory:
# 返回文件所在路径和文件名
fpath = os.path.join(directoryName,fname)
if not os.path.isdir(fpath):
visitfile(fpath,args)
def searcher(startdir,searchkey):
global fcount,vcount,list_of_contain_lines
fcount = vcount = 0
list_of_contain_lines = []
os.path.walk(startdir,visitor,searchkey)
if __name__=='__main__':
# root=raw_input("type root directory:")
root = '/home/jiangbin/findJS'
key=raw_input("type key:")
searcher(root,key)
print 'Found in %d files,visited %d'%(fcount,vcount)
print list_of_contain_lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment