首页 / PYTHON / python 检索文件内容工具
python 检索文件内容工具
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python 检索文件内容工具,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4385字,纯文字阅读大概需要7分钟。
内容图文
![python 检索文件内容工具](/upload/InfoBanner/zyjiaocheng/769/2a90b4a72b274fd6b5bd40941662c2c6.jpg)
公司内部需求一个工具检索目录下的文件在另外的目录中使用次数, 用来优化包体的大小。
此代码效率并不高效, 另添加对应的 后缀检索。 用python 实现比较快速, 另还有缺点是只支持 utf-8 格式内容。
各位用到的可以自己摘一下。
(本人习惯使用c\c++,目前发现这种脚本类的确实实现方便,接口齐全, 我能想到的接口, 真的全都有, 用python 写工具, 应该是一个调试起来还可以的过程)
代码如下:
1 #coding=utf-8 2 3 import os 4 import sys 5 import time 6 7 #private: 8 def checkSuffix(strFileName, strCompareSuffix): 9 strPreFileName = '' 10 strFileSuffix = '' 11 bIsCheckOk = False 12 strArray = strFileName.split('.') 13 if len(strArray) > 1: 14 strPreFileName = strArray[0] 15 strFileSuffix = strArray[1] 16 #print(strFileSuffix) 17 #print(strCompareSuffix) 18 if strCompareSuffix == '*' or strFileSuffix == strCompareSuffix: 19 bIsCheckOk = True 20 return bIsCheckOk, strPreFileName 21 22 def readFileName(file_dir): 23 for root, dirs, files in os.walk(file_dir): 24 return files,dirs, root 25 return '', '', '' 26 27 def findString(pathFile, findKey): 28 #print("open pathFile:", pathFile) 29 fp = open(pathFile, "r", encoding='utf-8') 30 strr = fp.read() 31 if(strr.find(findKey) != -1): 32 return True 33 return False 34 35 def startFind(files, dirs, root, findKey, strSuffix): 36 for fileName in files: 37 try: 38 bIsCheckOk, strPreFileName = checkSuffix(fileName, strSuffix) 39 if bIsCheckOk == False: 40 #print("fileName", fileName, " is not suffx :", strSuffix) 41 continue 42 if(findString(root + "\\" + fileName, findKey)): 43 return True, fileName 44 except Exception as err: 45 continue 46 47 for jj in dirs: 48 fi, di, ro = readFileName(root + jj) 49 bIsFind, fileName = startFind(fi, di, ro, findKey, strSuffix) 50 if(bIsFind == True): 51 return bIsFind, fileName 52 return False, '' 53 54 #public: 55 def findUse(dirPath, findKey, strSuffix): 56 files, dirs, root = readFileName(dirPath) 57 return startFind(files, dirs, root, findKey, strSuffix) 58 59 def getDirsFiles(dirPath): 60 dirfiles = []; 61 for root, dirs, files in os.walk(dirPath): 62 dirfiles += files; 63 return dirfiles 64 65 def writeResult(strFileName, strWriteSign, list): 66 fp = open(strFileName, 'a+') 67 fp.write(strWriteSign) 68 #fp.writelines(list) 69 for str in list: 70 fp.write(str) 71 fp.write('\n') 72 fp.close() 73 74 if __name__ == '__main__': 75 """ 76 findDir = u"E:\\mmo2018001\\artist\\open\\ui\\free\\" 77 findAimDir = u"E:\\mmo2018001\\artist\\open\\effect\\" 78 findsuffix = "prefab" # 哪些要搜索的文件的后缀 79 findAimsuffix = "*" # 搜索那些后缀的文件 80 """ 81 bt = time.clock() # 记录时间 82 83 findDir = sys.argv[1] 84 findAimDir = sys.argv[2] 85 findsuffix = sys.argv[3] 86 findAimsuffix = sys.argv[4] 87 88 thisPath = os.getcwd() 89 print("this path is ", thisPath) 90 print(findDir) 91 print(findAimDir) 92 print(findsuffix) 93 print(findAimsuffix) 94 95 dirFiles = getDirsFiles(findAimDir) 96 useFiles = [] 97 notUseFiles = [] 98 nLen = len(dirFiles) 99 i = 0 100 for filesName in dirFiles : 101 102 bIsCheckOk, strPreFileName = checkSuffix(filesName, findsuffix) 103 if bIsCheckOk == True: 104 isFind, fileName = findUse(findDir, strPreFileName, findAimsuffix) 105 #print("filesName:\t", filesName, " \nIsFind:\t\t", isFind) 106 if(isFind): 107 useFiles.append(filesName) 108 else: 109 notUseFiles.append(filesName) 110 i += 1 111 p = round(i * 100 / nLen) 112 duration = round(time.clock() - bt, 2) 113 remaining = round(duration * 100 / (0.01 + p) - duration, 2) 114 print("进度:{0}%,已耗时:{1}s,预计剩余时间:{2}s".format(p, duration, remaining), end="\r") 115 116 writePath = thisPath + "\\Result.txt" 117 try: 118 os.remove(writePath) 119 except Exception as err: 120 print(err) 121 writeResult(writePath, "***************NotUseFiles:***************\n", notUseFiles) 122 writeResult(writePath, "***************UseFiles:***************\n", useFiles) 123 useTime = time.clock() - bt 124 print("已完成 总耗时:", useTime)
内容总结
以上是互联网集市为您收集整理的python 检索文件内容工具全部内容,希望文章能够帮你解决python 检索文件内容工具所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】