当递归(recursion)遇到yield

2018-07-23  本文已影响86人  Rokkia
def copy_files(path, dis_path):
    files = os.listdir(path)
    for file in files:
        if os.path.isdir(os.path.join(path, file)):
            os.mkdir(os.path.join(dis_path, file))
            #这里实现递归 但是需要在递归函数前 + yield from
            yield from copy_files(os.path.join(path, file), os.path.join(dis_path, file))
        else:
            name, ext = os.path.splitext(file)
            if ext != '.webp':
                ma = re.match(r'.*_big$', name)
                if ma:
                    shutil.copy(os.path.join(path, file), os.path.join(dis_path, file))
                    #这里使用真正的yield
                    yield '{}       to      {}'.format(os.path.join(path, file), os.path.join(dis_path, file))

上一篇下一篇

猜你喜欢

热点阅读