zoukankan      html  css  js  c++  java
  • grep -rl 'python' /root

    # grep -rl 'python' /root  搜索root目录下文件内容包含python的文件名路径
    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)
                    target.send(file_abs_path)
    
    
    @init
    def opener(target):
        while True:
            file_abs_path = yield
            with open(file_abs_path,'r',encoding='utf-8') as f:
                target.send((file_abs_path,f))
    
    
    @init
    def cat(target):
        while True:
            file_abs_path,f = yield
            for line in f:
                tag =target.send((file_abs_path,line))   #注意:对于需要传两个yield的,在send时需要将这两个值放在一个元组中传递(())
                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'D:Python_OldBoy课程day5day5a'
    g = search(opener(cat(grep(printer(),'python'))))
    g=  search(opener(cat(grep(printer(),'python'))))
    g.send(x)
    

      

  • 相关阅读:
    [洛谷P2523] HAOI2011 Problem c
    [CF156D] Clues
    [洛谷P4769] NOI2018 冒泡排序
    [CF605E] Intergalaxy Trips
    [洛谷P4492] HAOI2018 苹果树
    [洛谷P3349] ZJOI2016 小星星
    [洛谷P4336] SHOI2016 黑暗前的幻想乡
    [洛谷P5364] SNOI2017 礼物
    [洛谷P2606] ZJOI2010 排列计数
    [洛谷P6078] CEOI2004 candy
  • 原文地址:https://www.cnblogs.com/wangkc/p/6938913.html
Copyright © 2011-2022 走看看