# 读取路径下所有的yaml文件,批量替换指定的字符串 import os import yaml import glob import re # 指定要替换的字符串和新的字符串 old_string = 'NestedMusicTransformer' new_string = 'AmadeusModel' # 指定yaml文件所在的目录 directory = 'Amadeus/symbolic_yamls/nn_params' # 遍历目录下的所有yaml文件 for filepath in glob.glob(os.path.join(directory, '*.yaml')): with open(filepath, 'r', encoding='utf-8') as file: content = file.read() # 使用正则表达式替换指定的字符串 new_content = re.sub(r'\b' + re.escape(old_string) + r'\b', new_string, content) # 将修改后的内容写回文件 with open(filepath, 'w', encoding='utf-8') as file: file.write(new_content) print(f'Processed file: {filepath}')