笔者发现,在训练样本时,有时需要对数据集的名字,进行统一的命名,以方便后续处理。笔者在工作中,训练niftynet的需要,写了一个小的python程序,来对文件进行重命名。下面直接上代码。
import os
'''
It is used to change a file name.
'''
new_name=[]
img_path = 'your file path'
# getting images' name
img_name = os.listdir(img_path)
for i, old_name in enumerate(img_name):
#replace the image name string with specified strings
new_name = old_name.replace('a', 'b')#a,old name;b,new name
#replace old_name with new_name
os.rename(img_path+"\\"+old_name,img_path+"\\"+new_name)
代码还是很简单,仅供有需要的参考。