正则匹配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()