0

I'm trying to iterate through a directory of multiple YAML files. My goal is to all merge them together, or rather, append them. My current solution requires me to load a 'placeholder.yml' file, which i then populate with the files in the directory. Here's the code:

import yaml
from yaml import Loader
import os


def yaml_loader(filepath):
    # Loads a yaml file
    with open(filepath, 'r') as file_descriptor:
        data = yaml.load(file_descriptor, Loader)
    return data


rootdir = './yaml_files'
generated_yaml = yaml_loader('./yaml_files/placeholder.yml')
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        data = yaml_loader(os.path.join(subdir, file))
        generated_yaml.update(data)

print(generated_yaml)

This solution is not satisfactory as the placeholder.yml and must hold at least one value. Is there a way to generate an empty YAML object for me to populate with the data i collect in my directory? Also, if you know of any Libary that would suit this requirement, please let me know

Thanks in advance

2 Answers2

0

yaml.load doesn't produce a "yaml object" as such, it just produces output that happens to correspond to the yaml in your file (or your string, if you load from a string). So it might produce a string, a list or a dict. Given that you're using update I'm guessing your yaml produces a dict, so you should be able to simply create an empty dict instead of using your placeholder:

rootdir = './yaml_files'
generated_yaml = dict()
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        data = yaml_loader(os.path.join(subdir, file))
        generated_yaml.update(data)
Ben Taylor
  • 483
  • 2
  • 13
0

I've decided to go with the !include functionality in PyYAML. This required a little bit of restructuring, but suits my needs better. The update function I used before, will overwrite configurations. As I only wanted to append files, using includes is way simpler. See here.