# Replaces exactly one occurrence of OLD by NEW in FILE (fails if OLD is absent or not unique). Args: FILE OLD NEW import sys f, old, new = sys.argv[1:4] s = open(f).read() if s.count(old) != 1: sys.exit(f"replace.py: {old!r} found {s.count(old)} times in {f}") open(f, "w").write(s.replace(old, new))