|
3 | 3 |
|
4 | 4 | import re
|
5 | 5 |
|
6 |
| -def read_file(relative_path, backup_dirs=None, **kwargs): |
| 6 | +def read_file(relative_path, backup_dirs=None, encoding="utf-8", **kwargs): |
7 | 7 | if backup_dirs is None:
|
8 | 8 | backup_dirs = []
|
9 | 9 |
|
10 | 10 | # Try to get the absolute path for the file from the original directory or backup directories
|
11 | 11 | absolute_path = find_file_in_dirs(relative_path, backup_dirs)
|
12 | 12 |
|
13 | 13 | # Read the file content
|
14 |
| - with open(absolute_path) as f: |
| 14 | + with open(absolute_path, 'r', encoding=encoding) as f: |
15 | 15 | content = remove_code_fences(f.read())
|
16 | 16 |
|
17 | 17 | # Replace placeholders with values from kwargs
|
@@ -63,10 +63,10 @@ def find_file_in_dirs(file_path, backup_dirs):
|
63 | 63 | def remove_code_fences(text):
|
64 | 64 | return re.sub(r'~~~\w*\n|~~~', '', text)
|
65 | 65 |
|
66 |
| -def write_file(relative_path:str, content:str): |
| 66 | +def write_file(relative_path:str, content:str, encoding:str="utf-8"): |
67 | 67 | abs_path = get_abs_path(relative_path)
|
68 | 68 | os.makedirs(os.path.dirname(abs_path), exist_ok=True)
|
69 |
| - with open(abs_path, 'w') as f: |
| 69 | + with open(abs_path, 'w', encoding=encoding) as f: |
70 | 70 | f.write(content)
|
71 | 71 |
|
72 | 72 | def delete_file(relative_path:str):
|
|
0 commit comments