赞
踩
import os # 查找所有没有用到的layout def findLayout(path): filePath = "F:/workspace/android/app/src/main" keyWord = ["R.layout.","@layout/"] # 记录drawable文件夹下的XML文件名(不带路径) stringUsedLayout = [] # 记录所有的图片(全路径名) stringLayout = [] string1 = [] with open(path, 'a', encoding='utf-8') as wf: # 遍历当前目录 for root, dirs, files in os.walk(filePath): # 遍历文件 for file in files: # pathFiles 是路径+文件名=绝对路径数组 pathFiles = os.path.join(root, file) # 查找drawable目录下的xml文件 if "\\layout\\" in pathFiles: stringLayout.append(pathFiles) if ".java" in pathFiles or ".xml" in pathFiles: # 读取每个文件 with open(pathFiles, 'r', encoding='utf-8') as w: # 读取文件的每一行 for lines in w: for key in keyWord: if key in lines: str = lines.split(key)[1] if ")" in str: str = str.split(")")[0] if ";" in str: str = str.split(";")[0] if "," in str: str = str.split(",")[0] if "\"" in str: str = str.split("\"")[0] if str not in stringUsedLayout: stringUsedLayout.append(str) for s in stringLayout: str = s.split("\\layout\\")[1].split(".xml")[0]; if str not in stringUsedLayout and str not in string1: string1.append(str) wf.write(str) wf.write("\n") # 删除未使用到的布局文件 os.remove(s) if __name__ == '__main__': path = "E:/text/Content.txt" findLayout(path)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。