Python对文件进行批量重命名

做caffe的训练数据的时候,有时候需要对图片进行批量重命名,脚本如下,

import os

def rename_jpg(main_path, i):
    jpg_list = os.listdir(main_path)
    for jpg in jpg_list:
        if jpg.endswith(".jpg"):
            src = os.path.join(main_path, jpg)
            new_name = str(i) + ".jpg"
            i = i + 2
            dst = os.path.join(main_path, new_name)
            print(src)
            print(dst)
            os.rename(src, dst)


if __name__ == "__main__":
    rename_jpg(r"/data/test1", 0)#命名为偶数的,0,2,4,,,
    rename_jpg(r"/data/test2", 1)#命名为奇数的,1,3,5,7,,,

 

posted @ 2020-03-10 15:14  cumtchw  阅读(329)  评论(0编辑  收藏  举报