用python找出ccb里面的所有图片,并复制到指定文件夹

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 很陌生,但通过一天的学习,居然 把这个小功能搞出来了,还是相当有成就感啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值