Created
July 8, 2016 07:41
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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