首页 技术 正文
技术 2022年11月9日
0 收藏 638 点赞 2,781 浏览 1477 个字

其实这个面向过程编写程序,是编写程序的基础,所以一定要好好掌握

此程序涉及知识点:装饰器,生成器,协程器应用

 # 编辑者:闫龙
import os
Distinct = [] #定义一个列表用于判断重复的文件
def AutoNext(Target): #生成器的Next装饰器
def NextTarget(*args):
res = Target(*args) #res得到Target(*args)的执行结果(Target())
next(res)#让res进行一次next到yield的操作
return res#返回res当前的状态(next到yield的状态)
return NextTarget @AutoNext#调用生成器的Next装饰器
def InputGetPath(Target):
InputPath = yield #InputPath等待yield的返回值
PathGen = os.walk(InputPath)#将InputPath中的目录子目录和文件游走后返回列表[路径,[子目录],[文件]]
for i in PathGen:
for j in i[-1]:
FilePath ="%s\\%s"%(i[0],j) #将格式化好的路径传递给FilePath
Target.send(FilePath) #使用Send方式传值给Target中的yield @AutoNext
def OpenFile(Target):
while True:
F = yield#F等待yild的返回值,这里是由InputPath()中的Target.send传递过来的
with open(F) as f:#将F路径的文件打开赋值给f
Target.send((f,F))#由于最后要显示文件路径所以,这里要以元组的方式传递两个值给下一个Target中的yield,f是文件句柄,F是文件路径
#("asdfasdf","F:\\a\\a.txt")
@AutoNext
def CatFile(Target):
while True:
f,F = yield#上方的OpenFile已经将f和F值传递到了这里的yield并返回给f,F,既然OpenFile传递了两个值,这里也要用两个值接收
#f="asdfasdf",F="F:\\a\\a.txt"
for i in f :
Target.send((i,F))#与OpenFile中的send一样,将i的值和F的值传递给下一个Target中的yield @AutoNext
def GrepLine(Target,chioce):#chioce是用户输入的即将检索的关键字
while True:
line,F = yield#同样还是要用两个参数来接收yield的返回值
if (chioce in line):
Target.send(F) #这里就不需要传递两个值了,因为最后的Target只需要的到文件路径就可以了 @AutoNext
def PrintInfo():
while True:
F = yield
if(F not in Distinct): #当F这个路径值不存在Distinct中时将F追加到Distinct列表中
Distinct.append(F)
print(F) chioce = input("请输入你要检索的关键字:")
#这里的调用其实,不难,仔细分析一下就能很容易的理解了
Gene = InputGetPath(OpenFile(CatFile(GrepLine(PrintInfo(),chioce))))
try: #针对Stop告警的异常处理
Gene.send("F:\\a")
except StopIteration:
print("检索完成")
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,149
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,619
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,462
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,237
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,872
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,040