python 代码:
import os
import shutil
import traceback
def delete_folder(path):
if os.path.exists(path):
print(f"删除文件夹: {path}")
shutil.rmtree(path)
print("删除完成")
def delete_file(path):
if os.path.exists(path):
print(f"删除文件: {path}")
os.remove(path)
print("删除完成")
def clean_project(src_root):
for root, dirs, files in os.walk(src_root):
try:
# 删除.gradle文件夹
if ".gradle" in dirs:
dir_path = os.path.join(root, ".gradle")
dir_size = shutil.disk_usage(dir_path).used / (1024 ** 2)
if dir_size > 5:
delete_folder(dir_path)
else:
print(f"文件夹: {dir_path} 大小: {dir_size:.2f}M")
# 删除.idea文件夹
if ".idea" in dirs:
delete_folder(os.path.join(root, ".idea"))
# 删除.iml文件和local.properties文件
for file in files:
file_path = os.path.join(root, file)
if file.endswith(".iml") or file == "local.properties":
delete_file(file_path)
# 删除build文件夹
if "build" in dirs:
delete_folder(os.path.join(root, "build"))
# 删除libs文件夹
if "libs" in dirs:
delete_folder(os.path.join(root, "libs"))
# 删除androidTest文件夹
if "androidTest" in dirs:
delete_folder(os.path.join(root, "androidTest"))
# 删除test文件夹
if "test" in dirs:
delete_folder(os.path.join(root, "test"))
except:
traceback.print_exc()
if __name__ == "__main__":
src_root = r"F:\workcode\android_project"
clean_project(src_root)