Recursively find and replace text in files in a directory. Args: directory: The directory to search. find: The text to find. replace: The text to replace.
(directory: str, find: str, replace: str)
| 180 | |
| 181 | |
| 182 | def find_replace(directory: str, find: str, replace: str): |
| 183 | """Recursively find and replace text in files in a directory. |
| 184 | |
| 185 | Args: |
| 186 | directory: The directory to search. |
| 187 | find: The text to find. |
| 188 | replace: The text to replace. |
| 189 | """ |
| 190 | for root, _dirs, files in os.walk(directory): |
| 191 | for file in files: |
| 192 | filepath = os.path.join(root, file) |
| 193 | with open(filepath, "r", encoding="utf-8") as f: |
| 194 | text = f.read() |
| 195 | text = re.sub(find, replace, text) |
| 196 | with open(filepath, "w") as f: |
| 197 | f.write(text) |