前言
做RN游戏 有些图片是png图片. uI给的命名 不规范, 通过脚本给图片重命名
# encoding: utf-8
#!/usr/bin/python
import os
import re
#替换文件夹的名字,包括文件夹的字符串含有子字符串
def replaceDirName(rootDir, oldStr, newStr):
for dirpath, dirNames, fileNames in os.walk(rootDir, topdown = False):
for dirName in dirNames:
if oldStr in dirName:
dirNameOld = os.path.join(dirpath,dirName)
dirNameNew = os.path.join(dirpath,dirName.replace(oldStr,newStr))
print(dirNameOld + ' --> '+ dirNameNew)
os.rename(dirNameOld, dirNameNew)
#替换文件名
def replaceFileName(rootDir, oldStr, newStr):
for dirpath, dirNames, fileNames in os.walk(rootDir):
for fileName in fileNames:
if oldStr in fileName:
fileNameOld = os.path.join(dirpath, fileName)
fileNameNew = os.path.join(dirpath,fileName.replace(oldStr, newStr))
print(fileNameOld + ' --> '+ fileName)
os.renames(fileNameOld, fileNameNew)
#替换文件中的内容
def replaceFileContent(rootDir,oldStr,newStr):
for dirpath,dirNames,fileNames in os.walk(rootDir):
for fileName in fileNames:
fileObj = os.path.join(dirpath,fileName)
f = open(fileObj,'r+')
all_the_lines=f.readlines()
f.seek(0)
f.truncate()
for line in all_the_lines:
f.write(line.replace(oldStr,newStr))
f.close()
#执行流
if __name__ == '__main__':
try:
rootDir = r"/Users/wanggang/Downloads/Q-打弱鸡游戏切图/@2X切图 - 副本/汪刚负责"
oldStr = "r弱鸡被打"
newStr = "beated_slow"
# replaceDirName(rootDir, oldStr, newStr)
# print("输入转换的目录")
# rootDir = raw_input() ;
# # rootDir = r"/Users/wanggang/Downloads/Q-打弱鸡游戏切图"
# print("输入旧的名字")
# oldStr = raw_input() ;
# print("输入新的名字")
# newStr = raw_input() ;
replaceFileName(rootDir, oldStr, newStr)
pass
except Exception as e:
print(e)
网友评论