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