Python脚本程序实现搜索文本文件内容
生活随笔
收集整理的這篇文章主要介紹了
Python脚本程序实现搜索文本文件内容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文介紹用Python實現的搜索本地文本文件內容的小程序。從而學習Python I/O方面的知識。代碼如下:
import os#根據文件擴展名判斷文件類型 def endWith(s,*endstring):array = map(s.endswith,endstring)if True in array:return Trueelse: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:",eelse: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:",eelse: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:",eelse: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:",eelse:#遍歷文件的每一行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!",returnfobj.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日志文件中,查看搜索結果。即每個關鍵在哪些文件中存在。并給出每個文件的具體路徑。?
附件:源代碼及配置文件
總結
以上是生活随笔為你收集整理的Python脚本程序实现搜索文本文件内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件操作中file.seek()方法
- 下一篇: EVM 互动百科