so I want to open each file in a directory (there are 5 plain text documents in this directory ). And do something like find specific words in each file.
This is code I used, but it seems the results are all stacked together instead by each file.
import re
import os
path = 'C:\Python27\projects\Alabama\New folder'
pattern = re.compile(r"\bshall\b")
pattern1 = re.compile(r"\bmay\b")
pattern2 = re.compile(r"\bmust\b")
for filenames in os.listdir(path):
with open(filenames, 'r') as myfile:
for string in myfile:
m = re.findall(pattern, string)
m1 = re.findall(pattern1, string)
m2 = re.findall(pattern2,string)
k = len(m)
k1 = len(m1)
k2 = len(m2)
print m,m1,m2,k,k1,k2
my question is that how to perform the re.findall function for every single one of the file in directory separately, instead of having a stacked up output.
Thanks!