您的位置:首页 > 编程语言 > Python开发

Python脚本01 —— 批量修改png图片文件名,增加@3x后缀

2015-07-01 12:08 786 查看
使用场景:

原文件名loading_0.png ~ loading_69.png, 要放到xcode的ImageAsset中,希望能够自动识别成3x的图片。

所以需要全部加上后缀@3x,变成loading_0@3x.png ~ loading_69@3x.png。

import os
import sys
import shutil

def file_extension(path):
return os.path.splitext(path)[1]

def file_name(path):
return os.path.splitext(path)[0]

def addSufix():
for f in os.listdir(srcDir):
file = os.path.join(srcDir, f)
print "find file: " + file
if os.path.isfile(file):
dstName = os.path.join(dstDir,file_name(f)+"@3x.png")
print "dst file :" + dstName
if (file_extension(file) == ".png") and (not os.path.exists(dstName)):
print "copyed dst file: " + dstName
#os.rename(oldname+".png",oldname+"@3x.png")
shutil.copy(file,dstName)

def createNewDir():
isExist = os.path.exists(dstDir)
if not isExist:
print "dir not exist. create dir: " + dstDir
os.mkdir(dstDir)
else:
print "dir exist: " + dstDir

srcDir = sys.path[0] + "/loading/"
dstDir = sys.path[0] + "/new/";
createNewDir()
addSufix()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python