【python】实现对文件夹中的图像连续重命名方法
import os
import shutildef rename_images(input_folder):# 获取输入文件夹下的所有图片文件(假设都是.jpg格式)image_files = [f for f in os.listdir(input_folder) if os.path.isfile(os.path.join(input_folder, f)) and f.endswith(".jpg")]# 如果文件夹为空,直接返回if not image_files:print("No images found in the folder.")return# 获取最大当前文件编号,从0开始max_num = len(image_files)new_names = list(range(1, max_num + 1))# 逐个重命名文件for i, file in enumerate(image_files):src_path = os.path.join(input_folder, file)dst_path = os.path.join(input_folder, "image" + '_'+str(new_names[i]) + ".jpg")shutil.move(src_path, dst_path)# 使用函数,提供你要重命名的文件夹路径
rename_images("E:\人工巡视相关样本等\样本留存1(Z+X)\工程车辆\image")
部分结果展示