From dc6dbce64da210b564e7191796540c75807fcb2b Mon Sep 17 00:00:00 2001 From: Chris Calloway Date: Mon, 12 Apr 2021 21:34:11 -0400 Subject: [PATCH] [#9] Remove before rename. --- publish_netcdf.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/publish_netcdf.py b/publish_netcdf.py index 630cd5f..f7cdde8 100755 --- a/publish_netcdf.py +++ b/publish_netcdf.py @@ -73,11 +73,17 @@ def replace_spaces_in_names(path): for root, dirs, files in walk: for f in files: if " " in f: - os.rename(os.path.join(root, f), os.path.join(root, f.replace(" ", "__"))) + replacement = os.path.join(root, f.replace(" ", "__")) + if pathlib.Path(replacement).exists(): + os.remove(replacement) + os.rename(os.path.join(root, f), replacement) replaced += 1 for d in dirs: if " " in d: - os.rename(os.path.join(root, d), os.path.join(root, d.replace(" ", "__"))) + replacement = os.path.join(root, d.replace(" ", "__")) + if pathlib.Path(replacement).exists(): + shutil.rmtree(replacement) + os.rename(os.path.join(root, d), replacement) replaced += 1 if replaced: logger.warning(f"Replaced {replaced} name{'s' if replaced != 1 else ''} " \