import os
def deco(func):
def wrapper(*args,**kwargs):
g=func(*args,**kwargs)
next(g)
return g
return wrapper
@deco
def search_file(target):
while True:
path=yield
g=os.walk(path)#g是生成器
for i in g:
for file_name in i[-1]:
file_path=r'%s\%s' %(i[0],file_name)
target.send(file_path)
@deco
def opener(target):
while True:
file_path=yield
with open(file_path,encoding='utf-8') as f:
target.send((file_path,f))
@deco
def reader(target):
while True:
file_path,f=yield
for line in f:
res=target.send((file_path,line)) #grep.send((file_path,line))
if res:
break
@deco
def grep(target,word):
tag = False
while True:
file_path, line = yield tag
tag = False
if word in line:
target.send(file_path)
tag=True
@deco
def printer():
while True:
file_path = yield
print(file_path)
g=search_file(opener(reader(grep(printer(),'root'))))
g.send(r'E:\PYTHON学习\excises\day9')