Don't change a file again if it's already been done.

When using `append_if_not_found=True` or `prepend_if_not_found=True`
the file will grow infinitely as replace() didn't check until now
whether the change has already been made.

This fixes this behavior (#18612).