Monday, August 4, 2014

Python code to search array of strings in particular folder

import os

#list of strings you want to find
apis = ["sandeepshabd@gmail.com",
"sandeep.shabd"
]

def find(word):
    def _find(path):
        with open(path, "rb") as fp:
            for n, line in enumerate(fp):
                if word in line:
                   yield word, n+1, line
    return _find

def search(word, start):
    finder = find(word)
    for root, dirs, files in os.walk(start):
        for f in files:
            path = os.path.join(root, f)
            for word, line_number, line  in finder(path):
                yield word, path, line_number, line.strip()

if __name__ == "__main__":
    import sys
 
    result =set()
    #Path of the document folder
    start= "/Users/sandeepshabd/Documents/"
    for api in apis:
        for word, path, line_number, line in search(api, start):
           result.add(word)
 
    print "**************************"
    for filterApi in list(result):
        print filterApi
    print "**************************"


No comments:

Post a Comment