From b2e7929119fdb3425e53d13f35bbf0be6259d630 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 20 Apr 2021 19:47:07 +0200 Subject: [PATCH] Update increment_path() to handle file paths (#2867) (cherry picked from commit c5c647e2816f70f17843755ecfc913a11e1d6492) --- utils/general.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utils/general.py b/utils/general.py index d8c8b504d311..abc587741658 100644 --- a/utils/general.py +++ b/utils/general.py @@ -594,14 +594,16 @@ def apply_classifier(x, model, img, im0): return x -def increment_path(path, exist_ok=True, sep=''): - # Increment path, i.e. runs/exp --> runs/exp{sep}0, runs/exp{sep}1 etc. +def increment_path(path, exist_ok=False, sep=''): + # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc. path = Path(path) # os-agnostic - if (path.exists() and exist_ok) or (not path.exists()): + if not path.exists() or exist_ok: return str(path) else: + suffix = path.suffix + path = path.with_suffix('') dirs = glob.glob(f"{path}{sep}*") # similar paths matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs] i = [int(m.groups()[0]) for m in matches if m] # indices n = max(i) + 1 if i else 2 # increment number - return f"{path}{sep}{n}" # update path + return f"{path}{sep}{n}{suffix}" # update path