Search code examples
pythonyamlpython-itertools

YAML load for chained file handles


PyYAML makes loading a .yml document trivial.

config = yaml.load(open('myconfig.yml', 'r'))

I have a list of YAML file names filelist, and I'm trying to come up with a concise Pythonic way of loading them together (treating them as a single document). This is what I tried.

fhlist = [open(filename, 'r') for filename in filelist]
cfginput = itertools.chain(*fhlist)
config = yaml.load(cfginput)

However, this is causing an exception.

AttributeError: 'itertools.chain' object has no attribute 'read'

What am I doing wrong? Is there a simple solution to the task I'm trying to accomplish?


Solution

  • I assume loading them together means either read them all, or merging them:

    Read them all

    yamls = []
    for filename in filelist:
        with open(filename) as f:
            yamls.append(yaml.load(f))
    

    Merge them all

    yamls = {}
    for filename in filelist:
        with open(filename) as f:
            yamls.update(yaml.load(f))
    

    Notice: This will overwrite properties with same keys, and is not recursive.

    Edit: If you want to concat all files and read them after as yaml:

    content = ''
    for filename in filelist:
        with open(filename) as f:
            content += f.read()
    result = yaml.load(content)
    

    This will probably get the same result as merging the yaml dicts...