Вы можете попробовать это:
содержимое demo_file1.yaml
:
fruits:
- bananas
- apples
- grapes
содержимое demo_file2.yaml
:
veg:
- cauli
- lady finger
- tomato
Код:
import yaml
import glob
files = glob.glob("/path/to/directory/*.yaml") # list of all .yaml files in a directory
def read_yaml_file(filename):
with open(filename, 'r') as stream:
try:
print(yaml.safe_load(stream))
except yaml.YAMLError as exc:
print(exc)
for file in files:
read_yaml_file(file)
выход:
{'fruits': ['bananas', 'apples', 'grapes']}
{'veg': ['cauli', 'lady finger', 'tomato']}