import json import os from pathlib import Path image_paths = os.listdir('../images') metadata = {} for image_path in image_paths: caption = Path(image_path).stem metadata[image_path] = caption with open('metadata.json', 'w') as f: json.dump(metadata, f, indent=4)