多文件,多目录下查询关键字脚本
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                多文件,多目录下查询关键字脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                python腳本作用:目錄下有很多的文件,或者存在二級甚至三級目錄,我們需要查文件當中的某一個關鍵字。
import os import re import sysdef listFiles(dirPath):fileList = [];for root, dirs, files in os.walk(dirPath):for fileObj in files:fileList.append(os.path.join(root,fileObj))return fileListdef findString(filePath, regex):f=open('rfi_basic.txt','a')fileObj = open(filePath, 'r')for eachLine in fileObj:if re.search(regex, eachLine, re.I):f.write(eachLine) #print eachLinedef main():reload(sys)sys.setdefaultencoding('utf-8')fileDir = "g:"+os.sep+"ips" //此處填寫目錄,os.sep表示/fileList = listFiles(fileDir) print fileListfor fileObj in fileList:findString(fileObj, ur"查找的關鍵字")//此處填寫要查找的關鍵字,不區分大小寫,可自行修改 # f.close() # os.system("pause")if __name__ == '__main__':main()?
轉載于:https://www.cnblogs.com/lcamry/p/5620566.html
總結
以上是生活随笔為你收集整理的多文件,多目录下查询关键字脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 云计算和虚拟机基础梳理
- 下一篇: HTML 图片标签
