国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現搜索文本文件內容腳本

2020-02-15 21:58:55
字體:
來源:轉載
供稿:網友

本文介紹用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("http://","http:////") #將路徑中的單反斜杠替換為雙反斜杠,因為單反斜杠可能會導致將路徑中的內容進行轉義了,replace函數中"http://"表示單反斜杠,"http:////"表示雙反斜杠    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("http:////","http://"))      #將這些搜索到的關鍵字從待搜索關鍵字列表中移除(后續將不再搜索該關鍵字)      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")            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 酒泉市| 漠河县| 延津县| 太谷县| 瑞丽市| 赤壁市| 浙江省| 西藏| 吕梁市| 栖霞市| 西峡县| 盐津县| 精河县| 嘉定区| 商南县| 叶城县| 鹤壁市| 丰原市| 康乐县| 洱源县| 绵竹市| 格尔木市| 新建县| 肃南| 长兴县| 栖霞市| 大同县| 湄潭县| 雷州市| 正镶白旗| 上虞市| 静安区| 淮安市| 吉木萨尔县| 泗阳县| 同德县| 丰城市| 遵义市| 台湾省| 筠连县|