from pathlib import Path
ROOT=Path(__file__).resolve().parents[2]
def read(path): return (ROOT/path).read_text(encoding='utf-8-sig')
def write(path, content):
    p=ROOT/path
    p.parent.mkdir(parents=True,exist_ok=True)
    p.write_text(content.strip()+'\n',encoding='utf-8')
    log=ROOT/'output/fixes-2026-10-20/changed.txt'
    with log.open('a',encoding='utf-8') as f: f.write(path+'\n')
def replace(path, old, new, count=-1):
    s=read(path)
    if old not in s: raise ValueError(f'Missing expected text in {path}: {old[:100]}')
    write(path,s.replace(old,new,count))
