使用方法
1.打開一個(gè)文本編輯器(如:sublime),把python代碼復(fù)制進(jìn)去,關(guān)閉保存為abc.py
2.打開abc.py
该默,修改相應(yīng)工程路徑配置住练,關(guān)閉保存
3.打開終端地啰,運(yùn)行sudo python /Users/longtiequan/Desktop/abc.py
4.文件名及類名替換完成,將文件重新拖入工程
一讲逛、修改文件前綴(JWCacheHelper-->HUANCUN)
# coding=utf-8
import os
sourthArr = [('THYBB', '/Users/wangzhe/Documents/PrivateJob/youbeibei',
'/Users/wangzhe/Documents/PrivateJob/youbeibei/DLapp.xcodeproj/project.pbxproj'),
('QOYQJ', '/Users/wangzhe/Documents/PrivateJob/TESTyouqianjin',
'/Users/wangzhe/Documents/PrivateJob/TESTyouqianjin/DLapp.xcodeproj/project.pbxproj')]
changeName = 'NetworkErrorView'
# 文件重命名函數(shù)髓绽,返回新的文件名
def file_rename(file_path):
root_path = os.path.split(file_path)[0] # 文件目錄
root_name = os.path.split(file_path)[1] # 文件名包含擴(kuò)展名
filename = os.path.splitext(root_name)[0] # 文件名
filetype = os.path.splitext(root_name)[1] # 文件擴(kuò)展名
new_path = os.path.join(root_path, filename.replace(pre_str, pre_to_str) + filetype) # 拼接新路徑
os.renames(file_path, new_path) # 文件重命名
return filename.replace(pre_str, pre_to_str)
if __name__ == "__main__":
for proName, jwproject_path, jwpbxpro_path in sourthArr:
# 需要修改的類名前綴 (需替換)
pre_str = changeName
# 新的類名前綴 (需替換)
pre_to_str = proName + pre_str
# 搜尋以下文件類型 (根據(jù)自己需求替換)
suf_set = ('.h', '.m', '.xib', '.storyboard', '.mm', 'swift')
# 項(xiàng)目路徑 (找到自己的項(xiàng)目路徑)
project_path = jwproject_path
# 項(xiàng)目project.pbxproj文件路徑 需要更新配置文件中的類名 (找到自己的項(xiàng)目project.pbxproj路徑)
pbxpro_path = jwpbxpro_path
# print(project_path, os.path.exists(project_path))
# print(pbxpro_path, os.path.exists(pbxpro_path))
# 定義一個(gè)字典 key=舊類名 value=新類名
needModifyDic = {}
# 遍歷文件,符合規(guī)則的進(jìn)行重命名
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
if file_name.startswith((pre_str,)) and file_name.endswith(suf_set):
old_name = os.path.splitext(file_name)[0]
new_name = file_rename(os.path.join(root, file_name))
needModifyDic[old_name] = new_name
# 遍歷文件妆绞,在文件中更換新類名的引用
# print(needModifyDic)
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
if file_name.endswith(suf_set):
# print('-----fileName-------' + file_name)
with open(os.path.join(root, file_name), 'r+') as f:
# print('========fileName========' + file_name)
s0 = f.read()
f.close()
for key in needModifyDic:
if key in s0:
with open(os.path.join(root, file_name), 'r+') as f4:
s1 = f4.read().replace(key, needModifyDic[key])
# print(key + ' ------> ' + needModifyDic[key])
f4.seek(0)
f4.write(s1)
f4.truncate()
f4.close()
# 替換配置文件中的類名
for key in needModifyDic:
with open(pbxpro_path, 'r+') as f:
s0 = f.read()
f.close()
if key in s0:
with open(pbxpro_path, 'r+') as f2:
s = f2.read().replace(key, needModifyDic[key])
f2.seek(0)
f2.write(s)
f2.truncate()
f2.close()
print(pre_to_str + '替換成功')
二顺呕、修改文件后綴(ViewModel-->VM)
# coding=utf-8
import os
# 需要修改的類名前綴 (需替換)
end_str = 'ViewModel'
# 不需要修改的類名后綴 (需替換)
not_end_str = []
# 新的類名后綴 (需替換)
end_to_str = 'VM'
# 搜尋以下文件類型 (根據(jù)自己需求替換)
suf_set = ('.h', '.m', '.xib', '.storyboard', '.mm')
# 項(xiàng)目路徑 (找到自己的項(xiàng)目路徑)
project_path = '/Users/****/Desktop/bank'
# 項(xiàng)目project.pbxproj文件路徑 需要更新配置文件中的類名 (找到自己的項(xiàng)目project.pbxproj路徑)
pbxpro_path = '/Users/****/Desktop/bank/bank.xcodeproj/project.pbxproj'
# 文件重命名函數(shù),返回新的文件名
def file_rename(file_path):
root_path = os.path.split(file_path)[0] # 文件目錄
root_name = os.path.split(file_path)[1] # 文件名包含擴(kuò)展名
filename = os.path.splitext(root_name)[0]; # 文件名
filetype = os.path.splitext(root_name)[1]; # 文件擴(kuò)展名
new_path = os.path.join(root_path, filename.replace(end_str, end_to_str) + filetype) # 拼接新路徑
# print(new_path)
os.renames(file_path, new_path) # 文件重命名
return filename.replace(end_str, end_to_str)
def end_check(name):
for str in not_end_str:
if name.find(str) != -1 or name == end_str:
return True
return False
# 定義一個(gè)字典 key=舊類名 value=新類名
needModifyDic = {}
# 遍歷文件括饶,符合規(guī)則的進(jìn)行重命名
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
fileName = os.path.splitext(file_name)[0]; # 文件名
if fileName.endswith(end_str) and file_name.endswith(suf_set) and end_check(fileName)==False:
# print(file_name)
old_name = os.path.splitext(file_name)[0]
new_name = file_rename(os.path.join(root, file_name))
needModifyDic[old_name] = new_name
# 遍歷文件株茶,在文件中更換新類名的引用
print(needModifyDic)
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
if file_name.endswith(suf_set):
print('-----fileName-------' + file_name)
with open(os.path.join(root, file_name), 'r+') as f:
print('========fileName========' + file_name)
s0 = f.read()
f.close()
for key in needModifyDic:
if key in s0:
with open(os.path.join(root, file_name), 'r+') as f4:
s1 = f4.read().replace(key, needModifyDic[key])
print(key + ' ------> ' + needModifyDic[key])
f4.seek(0)
f4.write(s1)
f4.truncate()
f4.close()
# 替換配置文件中的類名
for key in needModifyDic:
with open(pbxpro_path, 'r+') as f:
s0 = f.read()
f.close()
if key in s0:
with open(pbxpro_path, 'r+') as f2:
s = f2.read().replace(key, needModifyDic[key])
f2.seek(0)
f2.write(s)
f2.truncate()
f2.close()
三、修改資源文件MD5值图焰,不改變圖片(jpg,png)
# coding=utf-8
import hashlib
import os
# 獲取MD5
def GetFileMd5(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = open(filename,'rb')
while True:
b = f.read(8096)
if not b :
break
myhash.update(b)
f.close()
return myhash.hexdigest()
# 給文件添加末尾启盛,改變md5
def fileAppend(filename):
myfile = open(filename,'a')
# 添加一個(gè)自定義內(nèi)容,并不影響文件
myfile.write("jnethfjghjghj")
myfile.close
# 設(shè)置以這些結(jié)尾的
suf_set = ('.png', '.jpg')
project_path = '/Users/****/Desktop/bank'
# 遍歷文件技羔,符合規(guī)則的進(jìn)行重命名
# 項(xiàng)目路徑 (找到自己的項(xiàng)目路徑)
for (root, dirs, files) in os.walk(project_path):
for file_name in files:
if file_name.endswith(suf_set):
short_name = os.path.splitext(file_name)[0]
realpath = os.path.join(root, file_name)
print(short_name + ' ==> ' + realpath)
oldMd5 = GetFileMd5(realpath)
fileAppend(realpath)
newMd5 = GetFileMd5(realpath)
print(oldMd5 + '-->' + newMd5)