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)

  • 相关阅读:
    yanghui杨辉三角--(一维数组
    yanghui杨辉三角--(一维数组)探索1 2
    yanghui杨辉三角--(二维数组
    Fiber VS Coroutine VS Green Thread
    Java8-Reference
    Boolean
    Java-相等
    java.lang.Cloneable
    java.lang.CharSequence
    java.lang.AutoCloseable
  • 原文地址:https://www.cnblogs.com/xxonehjh/p/2936331.html
Copyright © 2011-2022 走看看