面向过程:核心是面向过程,过程即解决问题的步骤,基于面向过程去设计程序,就像是在设计流水线
#优点:程序结构清晰,可以把复杂的问题简单化,流程化
#缺点:可扩展性差,一条流水线只是用来解决一个问题
os模块功能展示:
1 #Author wangmengzhu 2 #装饰器 3 def init(func): 4 def wrapper(*args,**kwargs): 5 g = func(*args,**kwargs) 6 next(g) 7 return g 8 return wrapper 9 #第一阶段,找到所有文件的绝对路径 10 import os 11 @init 12 def search(target): 13 while True: 14 filepath = yield 15 g = os.walk(filepath) 16 for dirpath,_,files in g: 17 for file in files: 18 abspath = r'%s\%s'%(dirpath,file) 19 target.send(abspath) 20 21 22 #第二阶段,打开文件 23 @init 24 def opener(target): 25 while True: 26 abspath = yield 27 with open(abspath) as f: 28 target.send((abspath,f)) 29 30 31 #第三阶段,循环读出每一行的内容 32 @init 33 def cat(target): 34 while True: 35 abspath,f = yield 36 for line in f: 37 res = target.send((line,abspath)) 38 if res: 39 break 40 41 #第四阶段,过滤 42 @init 43 def grep(pattern,target): 44 tag = False 45 while True: 46 line,abspath = yield tag 47 tag = False 48 if pattern in line: 49 target.send(abspath) 50 tag = True 51 #第五阶段 52 @init 53 def printer(): 54 while True: 55 abspath = yield 56 print(abspath) 57 g = search(opener(cat(grep('error',printer())))) 58 g.send(r'C:UserswangmengzhuPycharmProjectsMZa')