editing.py 585 B

1234567891011121314151617
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import typing
  4. from pathlib import Path
  5. def append_to_file(filename: typing.Union[str, Path], what: str) -> None:
  6. with open(filename, 'a', encoding='utf-8') as f:
  7. f.write(what)
  8. def replace_in_file(filename: typing.Union[str, Path], search: str, replace: str) -> None:
  9. with open(filename, 'r', encoding='utf-8') as f:
  10. data = f.read()
  11. result = data.replace(search, replace)
  12. with open(filename, 'w', encoding='utf-8') as f:
  13. f.write(result)