使用Python清除多个Cocos Creator工程中的所有临时文件

1. 按

关于Cocos Creator工程的Git配置,可以参考:Cocos Creator工程 .gitignore的配置

2. 说明

新建一个文件夹,里面存储多个Cocos Creator工程,然后把脚本放到该目录下,运行即可把所有Cocos Creator工程里的所有临时文件全部清除(如果该工程正在使用Cocos Creator打开,则需要先关闭该工程)
在这里插入图片描述

3. 代码

3.1. clearCocosCreator.bat

python clearCocosCreator.py
pause

3.2. clearCocosCreator.py

import os
import shutil


dirs = ['build', 'library', 'temp', '.vscode', 'app']
files = ['.sln', '.csproj']

dirsCnt = 0
filesCnt = 0

def delWithCmd(path):
    try:
        if os.path.isfile(path):
            cmd = 'del "'+ path + '" /F'
            print(cmd)
            os.system(cmd)
    except Exception as e:
        print(e)


def deleteDir(dirPath):
    global dirsCnt
    global filesCnt
    for root, dirs, files in os.walk(dirPath, topdown=False):
        for name in files:
            try:
                filesCnt += 1
                filePath = os.path.join(root, name)
                print('file deleted', filesCnt, filePath)
                os.remove(filePath)
            except Exception as e:
                print(e)
                delWithCmd(filePath)
        for name in dirs:
            try:
                os.rmdir(os.path.join(root, name))
                dirsCnt += 1
            except Exception as e:
                print(e)
    os.rmdir(dirPath)

def delDir(dirPath):
    global dirsCnt
    shutil.rmtree(dirPath)
    dirsCnt += 1
    print('dir deleted', dirsCnt, dirPath)


def delFile(filePath):
    global filesCnt
    os.remove(filePath)
    filesCnt += 1
    print('file deleted', filesCnt, filePath)

def delete(path):
    try:
        if os.path.isfile(path):
            delFile(path)
        elif os.path.isdir(path):
            deleteDir(path)
    except Exception as e:
        print(e)

for proj in os.listdir():
    if not os.path.isdir(proj):
        continue
    os.chdir(proj)
    print(os.getcwd())
    for p in os.listdir():
        if os.path.isdir(p) and p in dirs:
            delete(p)
        elif os.path.isfile(p) and os.path.splitext(p)[1] in files:
            delete(p)
    localPath = 'local'
    if os.path.exists(localPath) and os.path.isdir(localPath):
        os.chdir(localPath)
        for p in os.listdir():
            if p == 'local.json':
                continue
            delete(p)
        os.chdir('..')
    packagesPath = 'packages'
    if os.path.exists(packagesPath) and os.path.isdir(packagesPath):
        os.chdir(packagesPath)
        print(os.listdir())
        if os.listdir() == []:
            os.chdir('..')
            os.rmdir(packagesPath)
    os.chdir('..')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

COCO56(徐可可)

建议微信红包:xucoco56

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值