Inspired by falsetru 's answer , I rewrote my code, making it more general.
Now the files to study:
can be described by a string as the second argument to be used glob(),
or using a function specially written for this purpose, if the set of desired files cannot be described using the globish pattern
and can be in the current directory if the third argument is not passed ,
or in the specified directory if its path is passed as the second argument
.
import re,glob
from itertools import ifilter
from os import getcwd,listdir,path
from inspect import isfunction
regx = re.compile('^[^\n]*word1.*?word3.*?$',re.S|re.M)
G = '\n\n'\
'MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW\n'\
'MWMWMW %s\n'\
'MWMWMW %s\n'\
'%s%s'
def search(REGX, how_to_find_files, dirpath='',
G=G,sepm = '\n======================\n'):
if dirpath=='':
dirpath = getcwd()
if isfunction(how_to_find_files):
gen = ifilter(how_to_find_files,
ifilter(path.isfile,listdir(dirpath)))
elif isinstance(how_to_find_files,str):
gen = glob.glob(path.join(dirpath,
how_to_find_files))
for fn in gen:
with open(fn) as fp:
found = REGX.findall(fp.read())
if found:
yield G % (dirpath,path.basename(fn),
sepm,sepm.join(found))
def select(fn):
return fn[-4:]=='.txt'
print ''.join(search(regx, select))
print ''.join(search(regx,'*.txt'))
sevral , ''.join() , ,
, , - ( ?)