import os
import re
import shutil
nativeDir = "filePath1/"
sharedDir = "filePath2"
needFindDir = sharedDir
g_fileCount = 0
targetRootDir = "./ccbFiles/"
if not os.path.exists(targetRootDir):
os.mkdir(targetRootDir)
fileList = os.listdir(needFindDir)
def copyFile(relativePathTable, fileDir):
for relativePath in relativePathTable:
dir = re.match(r"(.*)\/", relativePath)
if dir:
dir = "/" + dir.group(1)
else:
dir = ""
targetDir = fileDir + dir
if not os.path.exists(targetDir):
os.mkdir(targetDir)
absoluteNativePath = nativeDir + relativePath
ifFileExist = os.path.exists(absoluteNativePath)
if ifFileExist:
print("native file :", fileDir + relativePath)
global g_fileCount
g_fileCount = g_fileCount + 1
shutil.copy(absoluteNativePath, targetDir)
absoluteSharedPath = sharedDir + relativePath
ifFileExist = os.path.exists(absoluteSharedPath)
if ifFileExist:
print("shared file :", fileDir + relativePath)
global g_fileCount
g_fileCount = g_fileCount + 1
shutil.copy(absoluteSharedPath, targetDir)
for file in fileList:
if re.search(r"ccb", file):
fileName = re.match(r"(.*)\.ccb", file).group(1)
fileDir = targetRootDir + fileName
if not os.path.exists(fileDir):
os.mkdir(fileDir)
filePath = needFindDir + file
f = open(filePath, "r")
fileString = f.read()
relativePathTable = re.findall(r'>(.*\.[png,jpg]{1,3})<', fileString)
copyFile(relativePathTable, fileDir)
print("file count", g_fileCount)
工作中接触cocosbuilder比较多,有时候复制一个ccb,然后发现在ccb中引用 的资源要一个一个找出来,一起复制,不然会出现缺图,但是这个找图过程又相当 麻烦,要一个组件一个组件的去找,然后再复制。
今天早上突发奇想,要不要用python 搞一个脚本 ,来实现我想要的功能呢?
虽然对python还非常陌生,但对python的爬虫很有兴趣,于是,开始搞。
学习过程:
1、python 读取 文件
f = open(filePath, "r")
2、python 正则表达式
fileName = re.match(r"(.*)\.ccb", file).group(1)
relativePathTable = re.findall('>(.*\.png)<', fileString)
dir = re.match(r"(.*)\/", relativePath)
3、python 文件操作
os.path.exists(targetDir):
os.mkdir(targetDir)
shutil.copy(absoluteNativePath, targetDir)
os.listdir(needFindDir)
总结:虽然对python 很陌生,但通过一天的学习,居然 把这个小功能搞出来了,还是相当有成就感啊。