I have code that iterates through files recursively looking for word from a list. If found it then prints out the file it was found in, the string that was searched, and line it was found on.
My issue is that when searching for api
is also matches myapistring
, 'pass' matches 'compass', 'dev' matches 'device' instead of the actual word. So I need to implement a regex somewhere, but I'm unsure as to where and on which part of the for loop.
The regex I have got that I (think) works is:
regex='([\w.]+)'
rootpath=myDir
wordlist=["api","pass","dev"]
exclude=["testfolder","testfolder2"]
complist=[]
for word in wordlist:
complist.extend([re.compile(word)])
for path,name,fname in os.walk(rootpath):
name[:] = [d for d in name if d not in exclude]
for fileNum in fname:
i=path+"/"+fileNum
files.append(i)
for fileLine in files:
if any(ext in fileLine for ext in exten):
count=0
for line in open(fileLine, "r").readlines():
count=count+1
for lv in complist:
match = lv.findall(line, re.IGNORECASE)
for mat in match:
[print output]
Thanks
EDIT: Added this code as provided:
for word in wordlist:
complist.extend([re.compile('\b' + re.escape(word) + '\b')])
Which works with a few errors, but good enough that I can work with.