Skip to content

file_io

Utilities for handling IO operations.

update_file(filename, sub_line, strip=None)

Utility function for tasks to read, update, and write files

Source code in ci_cd/utils/file_io.py
def update_file(
    filename: Path, sub_line: tuple[str, str], strip: str | None = None
) -> None:
    """Utility function for tasks to read, update, and write files"""
    if strip is None and filename.suffix == ".md":
        # Keep special white space endings for markdown files
        strip = "\n"
    lines = [
        re.sub(sub_line[0], sub_line[1], line.rstrip(strip))
        for line in filename.read_text(encoding="utf8").splitlines()
    ]
    filename.write_text("\n".join(lines) + "\n", encoding="utf8")