mirror of
https://github.com/kevinveenbirkenbach/bulk-string-replacer.git
synced 2024-11-21 18:41:03 +01:00
Optimized string replace to catch utf8 errors
This commit is contained in:
parent
ecdc5ce920
commit
c7bf51941b
@ -2,18 +2,22 @@ import os
|
|||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
def replace_content(path, old_string, new_string, preview, verbose):
|
def replace_content(path, old_string, new_string, preview, verbose):
|
||||||
|
try:
|
||||||
with open(path, 'r', encoding='utf-8') as f:
|
with open(path, 'r', encoding='utf-8') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
if old_string in content:
|
if old_string in content:
|
||||||
new_content = content.replace(old_string, new_string)
|
new_content = content.replace(old_string, new_string)
|
||||||
|
|
||||||
print_verbose(f"Replacing content in: {path}",verbose)
|
print_verbose(f"Replacing content in: {path}", verbose)
|
||||||
|
|
||||||
if not preview:
|
if not preview:
|
||||||
with open(path, 'w', encoding='utf-8') as f:
|
with open(path, 'w', encoding='utf-8') as f:
|
||||||
f.write(new_content)
|
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):
|
def print_verbose(content,verbose):
|
||||||
if verbose:
|
if verbose:
|
||||||
print(content)
|
print(content)
|
||||||
|
Loading…
Reference in New Issue
Block a user