zoukankan      html  css  js  c++  java
  • day5_ grep -rl 'python' /root 功能 的实现

    import os

    def init(func):
    def wrapper(*args,**kwargs):
    res=func(*args,**kwargs)
    next(res)
    return res
    return wrapper

    @init
    def search(target):
    while True:
    search_path=yield
    g=os.walk(search_path)
    for par_dir,_,files in g:
    for file in files:
    file_abs_path=r'%s\%s' %(par_dir,file)
    # print(file_abs_path)
    target.send(file_abs_path)

    @init
    def opener(target):
    while True:
    file_abs_path=yield
    # print('opener func==>',file_abs_path)
    with open(file_abs_path,encoding='utf-8') as f:
    target.send((file_abs_path,f))

    @init
    def cat(target):
    while True:
    file_abs_path,f=yield #(file_abs_path,f)
    for line in f:
    tag=target.send((file_abs_path,line))
    if tag:
    break
    @init
    def grep(target,pattern):
    tag=False
    while True:
    file_abs_path,line=yield tag
    tag=False
    if pattern in line:
    tag=True
    target.send(file_abs_path)

    @init
    def printer():
    while True:
    file_abs_path=yield
    print(file_abs_path)



    x=r'C:UsersAdministratorPycharmProjectsday5a'



    g=search(opener(cat(grep(printer(),'python'))))
    print(g)

    g.send(x)
  • 相关阅读:
    too many open files linux服务器 golang java
    fasthttp 文档手册
    syncer.go
    grpc.go
    stm.go
    session.go
    mutex.go
    [HTML5]label标签使用以及建议
    禁止使用finalize方法
    [支付宝]手机网站支付快速接入
  • 原文地址:https://www.cnblogs.com/onda/p/6924315.html
Copyright © 2011-2022 走看看