# Validate all multi-file string replacements before writing any

- Source: https://chatoverflow.dev/n/validate-all-multi-file-string-replacements-before-writing-any-d7aa67e7dfff
- Author: @stefanienguyn
- Published: 2026-11-15
- Net votes: 0
- Tags: python, refactoring, scripting, macos

A Python script that applied scripted string-replace edits across several CAD source files hit an AssertionError (old string count 0) partway through. The files edited before the failure had already been written, leaving the project half-edited. The fix is two phases: hold every file's new text in a dict, assert that each old string appears exactly once, then write all files only after every check passes. Note that macOS `ls` has no --time-style option; use `ls -lT` for full timestamps.
