python實(shí)現(xiàn)搜索文本文件內(nèi)容腳本
本文介紹用python實(shí)現(xiàn)的搜索本地文本文件內(nèi)容的小程序。從而學(xué)習(xí)Python I/O方面的知識(shí)。代碼如下:
import os #根據(jù)文件擴(kuò)展名判斷文件類型 def endWith(s,*endstring): array = map(s.endswith,endstring) if True in array: return True else: return False #將全部已搜索到的關(guān)鍵字列表中的內(nèi)容保存到result.log文件中 def writeResultLog(allExistsKeywords): #行分隔符 ls = os.linesep #結(jié)果日志文件名 logfilename = "result.log" #相對(duì)路徑,文件在.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() #搜索指定關(guān)鍵字是否在指定的文件中存在 def searchFilesContent(dirname): #從searchkeywords.txt文件中初始化待搜索關(guān)鍵字列表 filename = "searchkeywords.txt" #相對(duì)路徑,文件在.py文件所在的目錄中 #待搜索關(guān)鍵字列表 allSearchKeywords=[] #遍歷文件當(dāng)前行已搜索到的關(guān)鍵字列表 existsKeywordsThisLine=[] #全部已搜索到的關(guān)鍵字列表 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函數(shù)去除每行的換行符 fobj.close(); #從excludekeywords.txt文件中初始化要排除的搜索關(guān)鍵字列表 filename = "excludekeywords.txt" #相對(duì)路徑,文件在.py文件所在的目錄中 #要排除的搜索關(guān)鍵字列表 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函數(shù)去除每行的換行符 fobj.close(); #從全部已搜索到的關(guān)鍵字列表排除掉不用搜索的關(guān)鍵字 for excluedkw in allExcludedKeywords: if(excluedkw in allSearchKeywords): allSearchKeywords.remove(excluedkw); #遍歷打開所有要在其中搜索內(nèi)容的文件,若待搜索關(guān)鍵字列表為空,則不再繼續(xù)遍歷 for root,dirs,files in os.walk(dirname): for file in files: if endWith(file,'.java','.xml','.properties'): #只在擴(kuò)展名為.java/.xml/.properties文件中搜索 #打開文件 filename = root + os.sep + file #絕對(duì)路徑 filename = filename.replace("\\","\\\\") #將路徑中的單反斜杠替換為雙反斜杠,因?yàn)閱畏葱备芸赡軙?huì)導(dǎo)致將路徑中的內(nèi)容進(jìn)行轉(zhuǎn)義了,replace函數(shù)中"\\"表示單反斜杠,"\\\\"表示雙反斜杠 try: fobj = open(filename,'r'); except IOError,e: print "*** file open error:",e else: #遍歷文件的每一行 for fileLine in fobj: #判斷當(dāng)前行是否包含所有搜索關(guān)鍵字 for keyword in allSearchKeywords: #若包含,并添加到該行已搜索到的關(guān)鍵字列表中 if keyword.upper() in fileLine.upper(): #將搜索關(guān)鍵字和該行文本內(nèi)容都轉(zhuǎn)換為大寫后再進(jìn)行匹配 existsKeywordsThisLine.append(keyword) #將這些搜索到的關(guān)鍵字添加到全部已搜索到的關(guān)鍵字列表中,并包含文件名信息 for keyword in existsKeywordsThisLine: allExistsKeywords.append(keyword+"\t"+filename.replace("\\\\","\\")) #將這些搜索到的關(guān)鍵字從待搜索關(guān)鍵字列表中移除(后續(xù)將不再搜索該關(guān)鍵字) for keyword in existsKeywordsThisLine: allSearchKeywords.remove(keyword) #清空該行已搜索到的關(guān)鍵字列表內(nèi)容 existsKeywordsThisLine = [] #若所有的關(guān)鍵字都搜索到了,則記錄日志文件,并結(jié)束搜索工作 if len(allSearchKeywords)==0: fobj.close(); writeResultLog(allExistsKeywords) print "DONE!", return fobj.close(); #全部文件遍歷結(jié)束 writeResultLog(allExistsKeywords) print "DONE!", #僅當(dāng)本python模塊直接執(zhí)行時(shí),才執(zhí)行如下語句,若被別的python模塊引入,則不執(zhí)行 if __name__ == '__main__': searchFilesContent(r"G:\ccsSmartPipe\SmartPipe\src\java")
1.筆者使用該程序?qū)ava項(xiàng)目中的源文件內(nèi)容進(jìn)行關(guān)鍵字的搜索。程序入?yún)樵擁?xiàng)目本地文件系統(tǒng)路徑G:\ccsSmartPipe\SmartPipe\src\java。
2.在配置文件中searchkeywords.txt中輸入要搜索的任意多個(gè)關(guān)鍵字
3.在配置文件中excludekeywords.txt中輸入在searchkeywords.
4.程序執(zhí)行完成后,即可在result.log日志文件中,查看搜索結(jié)果。即每個(gè)關(guān)鍵在哪些文件中存在。并給出每個(gè)文件的具體路徑。
附件:源代碼及配置文件
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在spyder IPython console中,運(yùn)行代碼加入?yún)?shù)的實(shí)例
這篇文章主要介紹了在spyder IPython console中,運(yùn)行代碼加入?yún)?shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python?八個(gè)數(shù)據(jù)清洗實(shí)例代碼詳解
不管你承不承認(rèn),數(shù)據(jù)清洗著實(shí)不是一件簡單的任務(wù),大多數(shù)情況下這項(xiàng)工作是十分耗時(shí)而乏味的,但它又是十分重要的,本篇文章帶給你八個(gè)實(shí)例代碼2022-01-01使用python裝飾器計(jì)算函數(shù)運(yùn)行時(shí)間的實(shí)例
下面小編就為大家分享一篇使用python裝飾器計(jì)算函數(shù)運(yùn)行時(shí)間的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04玩數(shù)據(jù)必備Python庫之numpy使用詳解
NumPy提供了許多高級(jí)的數(shù)值編程工具,如矩陣數(shù)據(jù)類型、矢量處理,以及精密的運(yùn)算庫,下面這篇文章主要給大家介紹了關(guān)于玩數(shù)據(jù)必備Python庫之numpy使用的相關(guān)資料,需要的朋友可以參考下2022-02-02Python采用Django制作簡易的知乎日?qǐng)?bào)API
這篇文章主要為大家詳細(xì)介紹了Python采用Django制作簡易的知乎日?qǐng)?bào)API,感興趣的小伙伴們可以參考一下2016-08-08