分享

python检索目录下所有文件中包含指定字符串的文件

 Andy517 2016-01-15

linux下grep命令就可以检索 配合其他遍历命令可以实现检索目录下文件中包含指定字符串的文件

windows下不知有没有这类工具,自己写个小工具

  1. import os  
  2. import re  
  3.   
  4. # list files  
  5. def listFiles(dirPath):  
  6.     fileList = [];  
  7.     for root, dirs, files in os.walk(dirPath):  
  8.         for fileObj in files:  
  9.             fileList.append(os.path.join(root,fileObj))  
  10.     return fileList  
  11.   
  12. def findString(filePath, regex):  
  13.     fileObj = open(filePath, 'r')  
  14.     for eachLine in fileObj:  
  15.         if re.search(regex, eachLine, re.I):  
  16.             print fileObj  
  17.             break  
  18.   
  19. def main():  
  20.     fileDir = "e:"+os.sep+"Package"  
  21.     regex = ur'FUNC_SYS_ADD_ACCDETAIL'  
  22.     fileList = listFiles(fileDir)  
  23.     for fileObj in fileList:  
  24.         findString(fileObj, regex)  
  25.     os.system("pause")  
  26.   
  27. if __name__ == '__main__':  
  28.     main()  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多