import os # 指定目录路径,替换为你的实际目录路径 directory = r'/home/chengyou/xcl/OminiControl_2conditions_hf5/result_context/COD10K' # 遍历目录中的所有文件 for filename in os.listdir(directory): if filename.endswith('.jpg'): # 分割文件名和扩展名 base = os.path.splitext(filename)[0] # 构建新的文件名 new_filename = base + '.h5' # 重命名文件 os.rename( os.path.join(directory, filename), os.path.join(directory, new_filename) ) print(f'Renamed: {filename} -> {new_filename}') print("All .jpg files have been renamed to .h5")