python实操(1):搜索指定目录(文件)下包含指定字符串的文件,及行号

#!/usr/bin/python
#encoding:utf-8
#  Filename : findString.py
#author by :morespeech
#python2.7
#platform:pycharm


import os
import re
class cFindString:
####public functions
    def __init__(self, filepattern , txtpattern):
        self.filePattern = re.compile(filepattern)
        self.txtParttern = re.compile(txtpattern)

    def findStringInFile(self, filename):
        if os.path.isdir(filename):
            for subfilename in os.listdir(filename):
                subfilename = os.path.join(filename, subfilename)
                self.findStringInFile(subfilename)
        else:
            if not os.path.exists(filename):
                print filename ,'not exist'
                return
            fileHandle = open(filename, 'rb')
            nLine = 0
            for line in fileHandle:
                search = self.txtParttern.search(line)
                if search:
                    print filename ,'(',nLine, ')', line
                nLine = nLine + 1
            fileHandle.close()

g_stringPattern=''
g_filePattern=''
g_filename=''

def parse():
    from optparse import OptionParser
    usage = "usage:findString [opthions] -n=.cpp -s=\d\w -f 1.cpp"
    parser = OptionParser(usage=usage)
    parser.add_option("-n", "--filenamePattern", dest = "wantfilePattern",help="match file format, .cpp/.c/.py etc")
    parser.add_option("-s", "--stringPattern", dest = "wantstringPattern", help="match string, regex etc")
    parser.add_option("-f", "--filename", dest = "wantfilename", help="input the file or folder you want to find")

    (options, args) = parser.parse_args()

    if options.wantfilePattern and options.wantstringPattern and options.wantfilename:
        global g_filePattern, g_stringPattern, g_filename
        g_filePattern = options.wantfilePattern
        g_stringPattern = options.wantstringPattern
        g_filename = options.wantfilename
    else:
         parser.print_help()


if __name__== "__main__":
    parse()
    cFindString(g_filePattern, g_stringPattern).findStringInFile(g_filename)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值