"""Replace one exact, unique string in a file (for pruning variants on working copies); fails if absent or not unique. Usage: python3 patch.py """ import sys path, old, new = sys.argv[1:4] text = open(path).read() if text.count(old) != 1: sys.exit(f"{path}: {text.count(old)} occurrences of {old!r}") open(path, "w").write(text.replace(old, new))