From f31b1b2705750a1c3a64c97b2dc0e2e4e065a6ab Mon Sep 17 00:00:00 2001 From: Harrison Date: Tue, 25 Apr 2023 09:15:26 -0500 Subject: [PATCH] Added check for non-existing keys in CSV dictionary --- renamebycsv/cli.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/renamebycsv/cli.py b/renamebycsv/cli.py index e7c2fe7..c9ce7e5 100755 --- a/renamebycsv/cli.py +++ b/renamebycsv/cli.py @@ -50,10 +50,17 @@ def rename( raise Exception("Duplicate current key.") replacement_dict[row[current_col_ind]] = row[become_col_ind] for subitem_path, subitem, match in candidates: + if match.group(1) not in replacement_dict: + logging.warning( + 'Group "%s" was not matched to any row in the provided CSV. ' + "Skipping...", + match.group(1) + ) + continue original = subitem_path objective = os.path.join( os.path.dirname(subitem_path), - re.sub(match.re, replacement_dict[match.group(1)], subitem), + re.sub(match.re, replacement_dict[match.group(1)], subitem.strip()), ) if keep_extension: objective += os.path.splitext(subitem_path)[1]