天天看点

python查找word关键词_Python 在指定的目录下搜索包含关键字的文件

#-*- coding:gbk -*-

'''

Created on 2012-3-13

@author: Czp

'''

import os

import shutil

class FindWord(object):

'''

Find the keyWork in input path

'''

def __init__(self):

'''

Constructor

'''

def _find(self,keyWord,dirName,fileList):

# print 'search ['+dirName+']'

keyWord = unicode(keyWord,'GBK')

for file in fileList:

path = os.path.join(dirName,file)

if os.path.isdir(path) and keyWord in path:

shutil.rmtree(path);

print 'find '+keyWord+' in '+path

def start(self,path,keyWord):

if os.path.exists(path):

os.path.walk(path, self._find, keyWord)

else:

print 'the path not exist'

if __name__=='__main__':

try:

fw = FindWord()

fw.start('D:\\Myprogram\\backUP','.svn')

except Exception,e:

print e