python目录文件迭代器yield所有文件

python中递归使用关键词yield

yield anthing;

python脚本

# -*-- coding:utf-8 -*--

import os

def get_recursive_file_list( path, base_name):
    current_files = os.listdir(path)
    for file_name in current_files:
        full_file_name = os.path.join(path, file_name)
        file_name = full_file_name.replace(base_name, '')

        if os.path.isdir(full_file_name):
            next_level_files = get_recursive_file_list(full_file_name, base_name)
            for files in next_level_files:
                yield files
        else:
            yield file_name

a = (get_recursive_file_list('d://test', 'd://test'))
for item in a:
    print(item)

0 comments

To reply to the article, please Login or registered