神剑山庄资源网 Design By www.hcban.com
本文介绍用python实现的搜索本地文本文件内容的小程序。从而学习Python I/O方面的知识。代码如下:
import os #根据文件扩展名判断文件类型 def endWith(s,*endstring): array = map(s.endswith,endstring) if True in array: return True else: return False #将全部已搜索到的关键字列表中的内容保存到result.log文件中 def writeResultLog(allExistsKeywords): #行分隔符 ls = os.linesep #结果日志文件名 logfilename = "result.log" #相对路径,文件在.py文件所在的目录中 try: fobj = open(logfilename,'w') except IOError,e: print "*** file open error:",e else: fobj.writelines(['%s%s' % (keyword,ls) for keyword in allExistsKeywords]) fobj.close() #搜索指定关键字是否在指定的文件中存在 def searchFilesContent(dirname): #从searchkeywords.txt文件中初始化待搜索关键字列表 filename = "searchkeywords.txt" #相对路径,文件在.py文件所在的目录中 #待搜索关键字列表 allSearchKeywords=[] #遍历文件当前行已搜索到的关键字列表 existsKeywordsThisLine=[] #全部已搜索到的关键字列表 allExistsKeywords=[] try: fobj = open(filename,'r'); except IOError,e: print "*** file open error:",e else: for eachLine in fobj: allSearchKeywords.append(eachLine.strip('\n')); #使用strip函数去除每行的换行符 fobj.close(); #从excludekeywords.txt文件中初始化要排除的搜索关键字列表 filename = "excludekeywords.txt" #相对路径,文件在.py文件所在的目录中 #要排除的搜索关键字列表 allExcludedKeywords=[] try: fobj = open(filename,'r'); except IOError,e: print "*** file open error:",e else: for eachLine in fobj: allExcludedKeywords.append(eachLine.strip('\n')); #使用strip函数去除每行的换行符 fobj.close(); #从全部已搜索到的关键字列表排除掉不用搜索的关键字 for excluedkw in allExcludedKeywords: if(excluedkw in allSearchKeywords): allSearchKeywords.remove(excluedkw); #遍历打开所有要在其中搜索内容的文件,若待搜索关键字列表为空,则不再继续遍历 for root,dirs,files in os.walk(dirname): for file in files: if endWith(file,'.java','.xml','.properties'): #只在扩展名为.java/.xml/.properties文件中搜索 #打开文件 filename = root + os.sep + file #绝对路径 filename = filename.replace("\\","\\\\") #将路径中的单反斜杠替换为双反斜杠,因为单反斜杠可能会导致将路径中的内容进行转义了,replace函数中"\\"表示单反斜杠,"\\\\"表示双反斜杠 try: fobj = open(filename,'r'); except IOError,e: print "*** file open error:",e else: #遍历文件的每一行 for fileLine in fobj: #判断当前行是否包含所有搜索关键字 for keyword in allSearchKeywords: #若包含,并添加到该行已搜索到的关键字列表中 if keyword.upper() in fileLine.upper(): #将搜索关键字和该行文本内容都转换为大写后再进行匹配 existsKeywordsThisLine.append(keyword) #将这些搜索到的关键字添加到全部已搜索到的关键字列表中,并包含文件名信息 for keyword in existsKeywordsThisLine: allExistsKeywords.append(keyword+"\t"+filename.replace("\\\\","\\")) #将这些搜索到的关键字从待搜索关键字列表中移除(后续将不再搜索该关键字) for keyword in existsKeywordsThisLine: allSearchKeywords.remove(keyword) #清空该行已搜索到的关键字列表内容 existsKeywordsThisLine = [] #若所有的关键字都搜索到了,则记录日志文件,并结束搜索工作 if len(allSearchKeywords)==0: fobj.close(); writeResultLog(allExistsKeywords) print "DONE!", return fobj.close(); #全部文件遍历结束 writeResultLog(allExistsKeywords) print "DONE!", #仅当本python模块直接执行时,才执行如下语句,若被别的python模块引入,则不执行 if __name__ == '__main__': searchFilesContent(r"G:\ccsSmartPipe\SmartPipe\src\java")
1.笔者使用该程序对java项目中的源文件内容进行关键字的搜索。程序入参为该项目本地文件系统路径G:\ccsSmartPipe\SmartPipe\src\java。
2.在配置文件中searchkeywords.txt中输入要搜索的任意多个关键字
3.在配置文件中excludekeywords.txt中输入在searchkeywords.
4.程序执行完成后,即可在result.log日志文件中,查看搜索结果。即每个关键在哪些文件中存在。并给出每个文件的具体路径。
附件:源代码及配置文件
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无python实现搜索文本文件内容脚本的评论...
更新日志
2024年10月02日
2024年10月02日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】