Optimized string replace to catch utf8 errors

This commit is contained in:
Kevin Veen-Birkenbach 2024-01-02 21:22:26 +01:00
parent ecdc5ce920
commit c7bf51941b
1 changed files with 13 additions and 9 deletions

View File

@ -2,17 +2,21 @@ import os
import argparse
def replace_content(path, old_string, new_string, preview, verbose):
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
if old_string in content:
new_content = content.replace(old_string, new_string)
print_verbose(f"Replacing content in: {path}",verbose)
if old_string in content:
new_content = content.replace(old_string, new_string)
if not preview:
with open(path, 'w', encoding='utf-8') as f:
f.write(new_content)
print_verbose(f"Replacing content in: {path}", verbose)
if not preview:
with open(path, 'w', encoding='utf-8') as f:
f.write(new_content)
except UnicodeDecodeError as e:
print_verbose(f"Warning: Unicode decode error encountered in file {path}. Skipping file.", verbose)
def print_verbose(content,verbose):
if verbose: