#!/usr/bin/python3 import os import json import subprocess from collections import OrderedDict #usage: python3 write_captionsJSON.py # do not overwrite existing captions if not os.path.exists("js/captions.json"): captions = {} else: captions = json.load(open("js/captions.json")) subprocess.call(['cp', 'js/captions.json', 'js/captions.json.back']) # CleanUp needed for renamed files (?how) # cleanup deleted images captions = { k : v for k,v in captions.items() if os.path.exists(k)} # For each album for path in [f for f in os.listdir('./') if os.path.isdir(f) and f != '!' and not 'videos' in f and f != 'js']: # For each image for name in [f for f in sorted(os.listdir(path)) if f.lower().endswith('.jpg')]: path_name = path + "/" + name if path_name not in captions.keys(): captions[path_name] = "" # write 'sorted dict' to file with open("js/captions.json","w") as j: json.dump(OrderedDict(sorted(captions.items())), j, indent=2)