INNER CODE UNIT · Python
read_file
1186258278/OpenClawChineseTranslation · scripts/inject_panel.py:58
def read_file(path):
"""读取文件内容"""
with open(path, 'r', encoding='utf-8') as f:
return f.read()
def write_file(path, content):
"""写入文件内容"""
with open(path, 'w', encoding='utf-8') as f:
f.write(content)
def upsert_marked_block(content, marker, block_content):
"""插入或更新带 marker 的注入块"""
block = f"{marker}\n{block_content}"
if marker in content:
pattern = re.compile(re.escape(marker) + r"[\s\S]*$", re.MULTILINE)
return pattern.sub(block, content), True
return content.rstrip() + "\n\n" + block + "\n", False