python 文本搜索

# coding: UTF-8

import os,sys,codecs
from stat import *

root = 'D:/hjhtemp/cdt8' #path
search = 'buildConsoleLines' #keyword
search_file_type = '.java' #file type

def worktree(top,callback):
    for f in os.listdir(top):
        pathname = os.path.join(top,f)
        mode = os.stat(pathname)[ST_MODE]
        if S_ISDIR(mode):
            worktree(pathname,callback)
        elif S_ISREG(mode):
            if f.endswith(search_file_type):
                callback(pathname)
        else:
            print('Skipping %s' % pathname)

def visitFile(filepath):
    file = codecs.open(filepath,'r','utf-8','replace')
    find = 0
    lineNum=0
    for line in file:
        lineNum=lineNum+1
        if search in line:
            print(line)
            find=1
            break
    if find == 1:
        print(filepath)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        search = sys.argv[1]
    worktree(root,visitFile)

posted @ 2013-02-28 10:07    阅读(326)  评论(0编辑  收藏  举报