这里,我写了一个简单的脚本,希望可以帮到大家,默认做的事情就是夺取当前目录下的所有以 png 结尾的文件,然后将 -hd.png 的文件自动重命名为 .png,如果发现有些文件没有对应的高清文件,那么就自动跳过,但是不用担心,我会把漏掉的那些文件显示在终端上,方便我们核对和进行后处理。
代码如下,我也放了一份在 github gist 上。
import os import re files = [] for (root, dirnames, filenames) in os.walk('.'): if len(filenames) != 0: for one in filenames: files.append(os.path.join(root, one)) isPng = re.compile(r'.*\.png$') isHdPng = re.compile(r'.*-hd\.png$') pngFiles = [] for f in files: if isPng.match(f): pngFiles.append(f) hdPngFiles = [] for f in files: if isHdPng.match(f): hdPngFiles.append(f) sdPngFiles = [] for f in pngFiles: if f not in hdPngFiles: sdPngFiles.append(f) # check whether these two lists cover the same files excludeFiles = [] for f in sdPngFiles: correspondingFile = re.sub(r'\.png$', r'-hd.png', f) if correspondingFile not in hdPngFiles: print f + ' doesn\'t have a corresponding hd file ' + correspondingFile excludeFiles.append(f) for f in hdPngFiles: correspondingFile = re.sub(r'-hd\.png$', r'.png', f) if correspondingFile not in sdPngFiles: print f + ' doesn\'t have a corresponding normal file ' + correspondingFile excludeFiles.append(f) print print 'Excluded Files:' for f in excludeFiles: if f in sdPngFiles: sdPngFiles.remove(f) if f in hdPngFiles: hdPngFiles.remove(f) print ' ' + f # replace! for f in hdPngFiles: correspondingFile = re.sub(r'-hd\.png$', r'.png', f) os.rename(f, correspondingFile) print 'Done!'