Embedded_game/001_修改json文件的imagePath.py
2025-01-02 12:48:11 +08:00

31 lines
1.0 KiB
Python

# -*- coding:utf-8 -*-
# @Author len
# @Create 2023/11/15 16:31
import os
import json
# Replace with the actual path to your folder containing the JSON files
json_directory = r'D:\Waste\嵌入式\数据集\车型\提亮'
# Loop through all the JSON files in the directory
for json_file in os.listdir(json_directory):
if json_file.endswith('.json'):
# Construct the full file path
json_file_path = os.path.join(json_directory, json_file)
# Read the JSON data
with open(json_file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# Update the 'imagePath' field with the JSON file's name, but with .png extension
new_image_path = json_file.replace('.json', '.png')
data['imagePath'] = new_image_path
# Write the updated JSON data back to the file
with open(json_file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
print("All 'imagePath' values have been updated.")