import os import json def add_image(json_file_path, image_url): """添加商品图像字段到指定的JSON文件""" # 读取JSON文件 with open(json_file_path, 'r', encoding='utf-8') as file: data = json.load(file) # 添加“商品图像”字段 data['商品图像'] = image_url # 将更新后的数据写回到JSON文件 with open(json_file_path, 'w', encoding='utf-8') as file: json.dump(data, file, ensure_ascii=False, indent=4) print("已成功添加“商品图像”字段。") if __name__ == "__main__": json_folder = "./database/meta" image_folder = "./database/image" for filename in os.listdir(json_folder): json_file_path = os.path.join(json_folder, filename) image_url = os.path.join(image_folder, filename.replace("png")) if os.path.exists(image_url): print(f"正在处理:{json_file_path}") # add_image(json_file_path, image_url) else: print(f"图片文件不存在:{image_url}")