zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    79.Word Search
    78.Subsets
    77.Combinations
    75.Sort Colors
    74.Search a 2D Matrix
    73.Set Matrix Zeroes
    71.Simplify Path
    64.Minimum Path Sum
    63.Unique Paths II
    Docker 拉取 oracle 11g镜像配置
  • 原文地址:https://www.cnblogs.com/xxonehjh/p/2936331.html
Copyright © 2011-2022 走看看