#!/usr/bin/env python3 import os from cfn_flip import flip, to_yaml for root, dirs, files in os.walk('.'): for f in files: if f.endswith('.json'): full_path = os.path.join(root, f) with open(full_path) as of: #s = of.read().strip() print(full_path) s = '' for line in of.readlines(): if line.strip().startswith('\"TemplateURL\"'): line = line.replace('.json', '.yaml') s += line cfn_yaml = to_yaml(s, clean_up=True) f2, ext = os.path.splitext(full_path) with open(f'{f2}.yaml', 'w+') as f3: f3.write(cfn_yaml) # do a "sed" on the yaml files to replace templateURL lines with open(f'{f2}.yaml', 'r+') as f4: s2 = '' for line2 in f4.readlines(): if line2.strip().startswith('TemplateURL'): line2 = line2.replace('.json', '.yaml') s2 += line2 f4.seek(0) f4.truncate() f4.write(s2)