INNER CODE UNIT · Python

replace_pattern_in_file

mtkennerly/ludusavi · tasks.py:24

def replace_pattern_in_file(file: Path, old: str, new: str, count: int = 1):
    content = file.read_text("utf-8")
    updated = re.sub(old, new, content, count=count)
    file.write_text(updated, "utf-8")


def confirm(prompt: str):
    response = input(f"Confirm by typing '{prompt}': ")
    if response.lower() != prompt.lower():
        sys.exit(1)


@task
def version(ctx):
    print(get_version())


@task

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…