公司內部需求一個工具檢索目錄下的文件在另外的目錄中使用次數, 用來優化包體的大小。python
此代碼效率並不高效, 另添加對應的 後綴檢索。 用python 實現比較快速, 另還有缺點是隻支持 utf-8 格式內容。c++
各位用到的能夠本身摘一下。app
(本人習慣使用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)