• 批量清理Unity项目Library目录 降低项目空闲占用空间


    Python程序运行截图 结尾云盘下载连接和源码

    在这里插入图片描述

    CSDN下载连接 底下有其它云盘下载连接
    https://download.csdn.net/download/qq_39162566/86977454

    运行环境:
    window

    python脚本版本:
    3.9

    运行方式:
    请使用管理员方式运行 以避免删除权限不足

    食用教程:
    遍历所有Unity项目后 需要确认是否执行删除 y/n y:继续执行清理 n:取消本次清理

    百度云盘连接

    链接:https://pan.baidu.com/s/1yP0T2n2Z64eKt1bTtH6Xgg
    提取码:7777

    蓝奏云

    https://wwf.lanzoue.com/i5wfm0fsmkab

    源码

    from os import path,remove,walk,system
    from traceback import print_exception
    import time
    from shutil import rmtree 
    
    def delFolder(url):
        '''
            删除文件夹
        '''
        if path.isdir(url):
            rmtree(url)
        else:
            remove(url)
     
    # def get_file_size(start_path = '.'):
    #     '''
    #         获取文件大小
    #     '''
        
    #     total_size = 0
    #     for dirpath, dirnames, filenames in os.walk(start_path):
    #         for f in filenames:
    #             fp = os.path.join(dirpath, f)
    #             # skip if it is symbolic link
    #             if not os.path.islink(fp):
    #                 total_size += os.path.getsize(fp)
    #     return total_size
    
    
    home_path = input("放入Unity 工程目录:\n")
    
    if not path.isdir(home_path):
        raise Exception(f"{home_path} 不是一个文件夹")
        
    deleteDirs = []
    # total_size = get_file_size(home_path)
    print("正在遍历查找所有Unity项目 请稍等...")
    for root, dirs, files in walk(home_path):
        if "Library" in dirs:
            libpath = path.join(root.replace('\\','/'),"Library")
            print(f"find one: {libpath}")
            deleteDirs.append(libpath)
    
    waitKey = input("y: 确认清理  n: 取消 \n")
    needCmdDir = []
    if "y" in waitKey:
        count = 0
        total = len( deleteDirs )
        start_time = time.time()
        for dir in deleteDirs:
            
            try:
                delFolder(dir)
                print(f"已删除 {dir}")
            except PermissionError as e:
                try:
                    print("正在尝试调用命令行 强制删除")
                    system(f'del "{dir}" /F /Q')
                    if not path.exists(dir):
                        print(f"已删除 {dir}")
                    else:
                        print(f"删除文件 {e.filename} 被阻止。请稍后手动删除")
                        needCmdDir.append(e.filename)
                except:
                    print(f"删除失败{dir}... 跳过 执行下一个")
            except:
                print_exception()
                print(f"删除失败{dir}... 跳过 执行下一个")
    
            count += 1
            print(f"当前进度: {count}/{total}")
        # print("正在计算已删除的文件总大小...")
        # nowfile_size = get_file_size(home_path)
        # print(f"删除之前: {total_size}Byte")
        # print(f"删除之后: {nowfile_size}Byte")
        # print(f"已删除的文件总大小: { ( total_size-nowfile_size ) / float(1024 * 1024) }MB")
        print("------------程序运行结束------------")
        print(f"用时: {time.time()-start_time}秒")
        if len( needCmdDir ) > 0 :
            print("------------------------------")
            print("以下文件或目录需要手动删除:")
            for dir in needCmdDir:
                print(dir)
    
    input("\n\n【任意键退出\n")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
  • 相关阅读:
    Intellij IDEA--Undo Commit,Revert Commit,Drop Commit的区别
    Python爬虫|Scrapy 基础用法
    如何让电脑永不息屏?Python:这事我熟,只需5行代码...
    家政小程序的开发:打造现代式便捷家庭服务
    标准库浏览 – Part II
    ASPICE项目实战
    【DockerCE】Docker-CE 20.10.18正式版发布
    SpringBoot保姆级教程(六)SpringBoot访问静态资源 & 整合JSP
    APP源码|智慧校园电子班牌源码 智慧校园云平台
    MySQL如何改进LRU算法
  • 原文地址:https://blog.csdn.net/qq_39162566/article/details/127825173