Python从csd文件中找到png引用

正则匹配Path=并拷贝匹配图片到新目录

#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os
import importlib,sys 
import shutil
importlib.reload(sys)
# sys.setdefaultencoding('utf8')
print (sys.getdefaultencoding())
  
def listFiles(dirPath):
	fileList=[]
	for root,dirs,files in os.walk(dirPath):
		for fileObj in files:
			fileList.append(os.path.join(root,fileObj))
	return fileList
 
def main():
	fileDir = os.getcwd() + "/old_csd"
	fileList = listFiles(fileDir)
 
	logData = open("./log.txt", 'w+')
	open("./findPng.js", 'w+')
 
	fData = open("findPng.js", "r+", encoding='UTF-8', errors='ignore')
	fData.seek(0, 0)
	fData.truncate()
	
	for fileObj in fileList:
		strCounter = 1

		f = open(fileObj,'r+', encoding='UTF-8', errors='ignore')
		print("\nopen file :" + os.path.basename(fileObj), file=logData)
		fileName = os.path.basename(fileObj).split('.')[0]
		all_the_lines=f.readlines()
		f.close() 
 
		numStrTable = []
		# numStrTable.append(fileName+' = {\n')
 
		if (os.path.basename(fileObj).split('.')[1] != "csd") :
			print("do nothing ... ... ...", file=logData)
			continue
		print("analysising ... ... ...", file=logData)
 
		for line in all_the_lines: 
			regex1 = re.compile(u"(Path=\".*)")
			regex2 = re.compile(u"(Path=\"\" Plist=\"\" \/\>)")
			regex3 = re.compile(u"(\.csd)")
			line11 = line.encode('utf8')	# temp line
			line1 = line11.decode('utf8')
 
			# ingore print and cclog
			results1 = regex1.findall(line1)
			length1 = len(results1)
			results2 = regex2.findall(line1)
			length2 = len(results2)
			results3 = regex3.findall(line1)
			length3 = len(results3)
			# iterate over all found quotes pairs
			if length2 == 0 and length3 == 0 and length1 > 0 :
				for match in regex1.finditer(line1):	
					start = match.start()
					end = match.end()	
					temp = line1[start:end]
					temp = temp.replace('Path=', '')
					temp = temp.replace(' Plist="" />', '')	
					numStrTable.append(temp+'\n')

					temp = temp.replace('\"', '')
					dirPath = os.getcwd() + '/new_csd_png/'+ temp
					targetPath = dirPath.split(os.path.basename(dirPath))[0]
					if not os.path.isdir(targetPath):
						os.makedirs(targetPath)

					shutil.copy(os.getcwd() + '/old_csd_png/'+ temp, dirPath)
					strCounter += 1
				print('find ------>' + line1, file=logData)
  
		# numStrTable.append('},\n')
		if strCounter > 1 :
			# write Localization data 
			for lines2 in numStrTable :
				fData.write(lines2)
	
	fData.close()  
 
if __name__=='__main__':
	main() 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值