Files
Python_CookBook_repo/4.迭代器与生成器/13.创建处理数据的管道.py
2025-09-10 16:12:45 +08:00

42 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from fnmatch import fnmatchcase
if __name__ == "__main__":
base_path = "D:\Code\Learn\practice"
# 想要建立一个处理管道需要将几个生成器函数堆叠比如我现在要输出所有practice文件夹下的注释行内容
# 1.首先生成文件夹路径
def gen_dir_path(fpath):
for path, dir, files in os.walk(fpath):
for file in files:
if (not fnmatchcase(path, "*.[git][idea]*")) and (not fnmatchcase(file, "*.md")):
yield os.path.join(path, file)
# 2.打开文件
def gen_file(paths):
for path in paths:
f = open(path, 'rt', encoding='utf-8')
yield f
f.close()
# 3.读取文件内容
def gen_file_txt(opened_files):
for file in opened_files:
yield from file
print("done")
# 4.匹配符合条件的文件行
def read_file_lines(file_lines):
for line in file_lines:
if "#" in line:
print(line)
# 将这些函数堆在一起就组成了一条管道,每次生成一个对象进行流水线处理,
# 这样避免了for循环的嵌套
paths = gen_dir_path(base_path)
files = gen_file(paths)
lines = gen_file_txt(files)
read_file_lines(lines)