java删除脚本怎么写_删除无用文件脚本

import os

# 赵鹏 删除项目中无用文件 有风险,例如

#mContext.getResources().getIdentifier("rooms_third_guard_diamond_" + bean.getGuardLevel(), "drawable", mContext.getPackageName()) 过滤掉这种情况

# style.xml中的情况未处理,需手动处理

rootPath = "D:\code\Shiliu_sixrooms_main"

javaDirSet = {""}#java目录的集合

resDirSet = {""}#res目录的集合

javaFileSet = {""}#.java .kt 文件的集合

resFileSet = {""}#drawable layout 目录下文件的集合

styleFileSet = {""}

def getFolderPath(rootPath):

if isinstance(rootPath, str):

if os.path.isdir(rootPath):

dirList = os.listdir(rootPath)

for path in dirList:

totalPath = os.path.join(rootPath, path)

if os.path.isdir(totalPath):

if "build" in path:

continue

if "java" == path:

# print("java -- folder -- " + totalPath)

javaDirSet.add(totalPath)

continue

if "res" == path:

# print("res -- folder -- " + totalPath)

resDirSet.add(totalPath)

continue

if os.path.isdir(totalPath):

getFolderPath(totalPath)

def getFilePath(folderPath):

if isinstance(folderPath, str):

if os.path.isdir(folderPath):

pathList = os.listdir(folderPath)

for path in pathList:

getFilePath(os.path.join(folderPath, path))

elif os.path.isfile(folderPath):

# print(folderPath )

if ".java" in folderPath or ".kt" in folderPath:

# print(folderPath)

javaFileSet.add(folderPath)

if "drawable" in folderPath or "layout" in folderPath:

resFileSet.add(folderPath)

if "styles.xml" in folderPath:

styleFileSet.add(folderPath)

getFolderPath(rootPath)

print("javaDirSet -- " + str(len(javaDirSet)))

for path in javaDirSet:

getFilePath(path)

print("resDirSet -- " + str(len(resDirSet)))

for path in resDirSet:

getFilePath(path)

print("resFileSet -- " + str(len(resFileSet)))

print("javaFileSet -- " + str(len(javaFileSet)))

# for path in javaFileSet:

# print(path)

totalNum = len(resFileSet)

curNum = 0;

for resFilePath in resFileSet:

curNum = curNum + 1

print("剩余条数 ------------ " + str(totalNum - curNum))

if os.path.exists(resFilePath):

# print("resFilePath -- " + resFilePath)

name = os.path.basename(resFilePath)

name = name[:name.index(".")]#截取文件名

isFileUsed = False;

# print(name)

# print("javaFileSet -- " + str(len(javaFileSet)))

# 查询java和kotlin文件是否使用

for path in javaFileSet:

# print("path -- " + path)

if len(path) > 0:

# print("path -- " + path)

if os.path.exists(path):

try:

with open(path, encoding= 'utf-8') as foo:

for line in foo.readlines():

if name in line:

isFileUsed = True

break

except FileNotFoundError as e:

e.filename

if isFileUsed:

break

# 查询drawable 和layout 中的.xml文件是否使用

if isFileUsed == False:

for resPath in resFileSet:

if len(resPath) > 0 and ".xml" in resPath:

# print("path -- " + path)

if os.path.exists(resPath):

try:

with open(resPath, encoding= 'utf-8') as foo:

for line in foo.readlines():

if name in line:

isFileUsed = True

break

except FileNotFoundError as e:

e.filename

if isFileUsed:

break

#查询styles.xml文件是否使用

if isFileUsed == False:

for stylePath in styleFileSet:

if len(stylePath) > 0 and ".xml" in stylePath:

# print("path -- " + path)

if os.path.exists(stylePath):

try:

with open(stylePath, encoding= 'utf-8') as foo:

for line in foo.readlines():

if name in line:

isFileUsed = True

break

except FileNotFoundError as e:

e.filename

if isFileUsed:

break

if isFileUsed == False:

print("remove -- " + resFilePath)

try:

# mContext.getResources().getIdentifier("rooms_third_guard_diamond_" + bean.getGuardLevel(), "drawable", mContext.getPackageName()) 过滤掉这种情况

name = os.path.basename(resFilePath)

name = name[:name.index(".")] # 截取文件名

numStr = name[name.rindex("_") + 1:]

# print("numstr ------- " + numStr)

if len(numStr) > 0 and numStr.isdigit():

print("------------ 不删除 ----------------")

else:

print("------------ 删除 ----------------")

os.remove(resFilePath)

except ValueError as e:

e.args

本文地址:https://blog.csdn.net/qingjiaowodiyiming/article/details/107367047

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值