-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodifyImageName.py
More file actions
127 lines (109 loc) · 3.46 KB
/
modifyImageName.py
File metadata and controls
127 lines (109 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# coding: utf-8
import os
import re
import time
# 前缀
PREFIXNAME = "PY_"
# 图片路径(绝对路径)
IMAGEPATH = "/Users/***/Desktop/项目名称/Assets.xcassets"
# 文件路径(绝对路径)
FILEPATH = "/Users/***/Desktop/项目名称"
# 图片文件名称后缀
IMAGEFILEEND = ".imageset"
# 图片名称后缀
IMGAENAMEEND = ".png"
#--------------------------------修改项目图片名称------------------------------------------#
# 修改项目图片名称
def changeProgectImageName():
getImageName(IMAGEPATH)
scanFilesContent(FILEPATH)
# 修改名称规则
def changeNameRule(oldName):
# return PREFIXNAME + oldName
return oldName[2:]
# 获取所有图片名称
def getImageName(path):
if not os.path.exists(path):
print("文件不存在" + path)
return
fileList = os.listdir(path)
for file in fileList:
if file.startswith("."):
continue
currentPath = os.path.join('%s/%s' % (path, file))
if currentPath.endswith(IMAGEFILEEND):
imageNameList.append(file[:-len(IMAGEFILEEND)])
currentPath = changeFilePathName(currentPath)
changeImageName(currentPath)
if os.path.isfile(currentPath):
continue
getImageName(currentPath)
# 修改文件名称
def changeFilePathName(oldPath):
if not os.path.exists(oldPath):
print("文件不存在" + oldPath)
return oldPath
pathPuple = os.path.split(oldPath)
newName = changeNameRule(pathPuple[1])
newPath = pathPuple[0] + "/" + newName
os.rename(oldPath, newPath)
print(oldPath)
print(newPath)
return newPath
# 修改图片名称
def changeImageName(path):
if not os.path.exists(path):
print("文件不存在" + path)
return
fileList = os.listdir(path)
imageList = []
for floder in fileList:
if floder.endswith(IMGAENAMEEND):
imageList.append(floder)
for item in imageList:
changeFilePathName(path + "/" + item)
replaceFileContent(path + "/Contents.json", [item])
# 替换文件内容
def replaceFileContent(path, contentList, isOCStr = bool(0)):
fileOpen = open(path)
w_str = ""
for line in fileOpen:
w_str += replaceLineContent(contentList, line, isOCStr)
writeOpen = open(path, 'w')
writeOpen.write(w_str)
fileOpen.close()
writeOpen.close()
# 替换行中内容
def replaceLineContent(contentList, line, isOCStr = bool(0)):
for item in contentList:
oldName = item
newName = changeNameRule(item)
if isOCStr:
oldName = "@\"" + oldName + "\""
newName = "@\"" + newName + "\""
if re.search(oldName, line):
line = re.sub(oldName, newName, line)
print(line)
return line
# 扫描文件内容
def scanFilesContent(path):
if not os.path.exists(path):
print("文件不存在" + path)
return
fileList = os.listdir(path)
for file in fileList:
if file.startswith("."):
continue
currentPath = os.path.join('%s/%s' % (path, file))
if currentPath.endswith(".m"):
replaceFileContent(currentPath, imageNameList, bool(1))
if os.path.isfile(currentPath):
continue
scanFilesContent(currentPath)
if __name__ == "__main__":
start = time.process_time()
# 修改项目图片名称
imageNameList = []
changeProgectImageName()
end = time.process_time()
print('Running time: %s Seconds' % (end - start))