本文主要是介绍Android 清除临时文件,清空缓存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python 代码:
import os
import shutil
import tracebackdef 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:for filename in files:apath = os.path.join(root, filename)# 文件大于10M就打印if os.path.getsize(apath) > 10 * 1024 * 1024:print(f"文件: {apath} 大小: {os.path.getsize(apath) / (1024 ** 2):.2f}M")# 删除.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)
这篇关于Android 清除临时文件,清空缓存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!