I have folder structure and I would like to create JSON objects based on the folder names in the root folder and the files below in the file tree. The file names are well structured like this:
objectNNN.xyz
Where NNN is a number like 001, 002 ... and xyz could be .png, .jpg, .eps or .mp3
the folder structure is like this (input to the script):
fruits
images
sounds
animals
foods
... based on this FOLDER structure, I'd like to read all the "sets" (fruits, animals, etc) and create a JSON object for each set like below: (note that the "word" key is take from all the object names in the images directory).
sets = {
animals: [ // this is from the folder name in the root folder
{
word: "cat", // this is from the filename in the images directory eg cat001.jpg
images: [
{
path: "images/basic/cat001.jpg"
}, {
path: "images/basic/cat002.jpg"
}
],
sounds: [ // based on all the images look for sounds
{
path: "sounds/basic/cat001.mp3"
}, {
path: "sounds/basic/cat002.mp3"
}
]
}, // etc more sets and words
The question is rather ill-defined, but here is my best shot at interpreting what you want. Extending this to adding file-paths and more groups of items is left as an exercise to the reader.
from re import split
from itertools import groupby, izip
fruits = set(["apple", "pear"])
animals = set(["bear", "cat"])
json_structure = {}
text_input = ["apple001.png", "apple002.jpg", "bear001.png", "pear001.png", "cat001.png"]
def check(filename):
"""Checks which group a filename is in and returns an integer for ordering"""
n = split(r'[0-9]', filename)[0]
if n in fruits:
return 0
else:
return 1
grouped = groupby(sorted(text_input, key=check), key=check)
for objects, object_type in izip(grouped, ["fruits", "animals"]):
items = objects[1]
json_structure[object_type] = list(items)
print json_structure