zoukankan      html  css  js  c++  java
  • 过滤莫文件夹下所有文件和子文件夹中的文件,并把路径打印出-----面对过程的编程

    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:
            dir_name = yield
           
    g = os.walk(dir_name)
            for i in g:
                for j in i[-1]:
                    if len(j.split('.'))==1:
                         file_path = '%s\%s'%(i[0],j)
                         #print(file_path)
                        
    target.send(file_path)

    @init
    def opener(target):
        '打开文件,获取文件句柄'
       
    while True:
            file_path = yield
            with
    open(file_path,encoding='utf-8') as f:
                target.send((file_path,f))

    @init
    def cat(target):
        '读取文件内容'
       
    while True:
            file_path,f=yield
            for
    line in f:
                target.send((file_path,line))
               
    @init
    def grep(pattern,target):
        '过滤一行内容中有无Python'
       
    while True:
            file_path,line = yield
            if
    pattern in line:
                target.send(file_path)
               
    @init
    def unrepeat(target):
        '去重相同的文件路径'
       
    file_path_l =''
       
    while True:
            file_path =yield
            if
    file_path !=  file_path_l:
                target.send(file_path)
                file_path_l=file_path

    @init
    def printer():
        '打印文件路径'
       
    while True:
            file_path = yield
           
    print(file_path)

    g = search(opener(cat(grep('error',unrepeat(printer())))))
    g.send('d:\Program Files(x86)\py_fallstack_s4')
  • 相关阅读:
    luogu P1455 搭配购买
    浅谈筛素数
    luogu P1205 方块转换
    luogu P2241 统计方形
    luogu P1866 编号
    luogu P1042 乒乓球
    4.7清明考试(完蛋)
    LINUX 启动图形界面和查看运行级别
    密钥登录LINUX步骤
    服务命令只支持基本的LSB操作(启动、停止、重新启动、尝试重启、重新加载、强制重新加载、状态)。对于其他操作,请尝试使用systemctl。
  • 原文地址:https://www.cnblogs.com/IQ-Python/p/6700335.html
Copyright © 2011-2022 走看看