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       


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章