From df49d43c50a913facb00694a20df67ea65ddba76 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 22 Aug 2023 22:39:36 +0200 Subject: [PATCH] show replace content just if to replace content exist --- replace_string.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/replace_string.py b/replace_string.py index e45d9f4..4370a20 100644 --- a/replace_string.py +++ b/replace_string.py @@ -5,14 +5,15 @@ def replace_content(path, old_string, new_string, preview, verbose): with open(path, 'r', encoding='utf-8') as f: content = f.read() - new_content = content.replace(old_string, new_string) - - if verbose: - print(f"Replacing content in: {path}") + if old_string in content: # Prüfen, ob der alte String im Inhalt vorkommt + new_content = content.replace(old_string, new_string) - if not preview: - with open(path, 'w', encoding='utf-8') as f: - f.write(new_content) + if verbose: + print(f"Replacing content in: {path}") + + if not preview: + with open(path, 'w', encoding='utf-8') as f: + f.write(new_content) def process_directory(base_path, old_string, new_string, recursive, folder, files, content, preview, verbose, hidden): for root, dirs, filenames in os.walk(base_path):