python重命名文件夹内的图片

import os
import shutil
import sys

src_dir = "/home/xxx/22-12-01/xxx_180to270/images"
dst_dir = "/home/xxx/22-12-01/xxx_180to270/images_dst"
if dst_dir is not os.path.exists:
    os.makedirs(dst_dir)
else:
    shutil.rmtree(dst_dir)

pic_list = os.listdir(src_dir)
for pic in pic_list:
    print("\nold name: {}".format(pic))
    pic_num = int(pic.split(".")[0].split("_")[-1])
    prefix = pic.split(".")[0].split('_')[0]
    pic_num -= 40
    # print("num: {}".format(pic_num))
    new_name = prefix + "_%08d.png" % pic_num
    print("new name: {}".format(new_name))

    shutil.copyfile(os.path.join(src_dir, pic),os.path.join(dst_dir, new_name))
    

你可能感兴趣的:(字符串/文件处理,python,开发语言)