glenn-jocher commited on
Commit
c5c647e
1 Parent(s): 1df8c6c

Update increment_path() to handle file paths (#2867)

Browse files
Files changed (1) hide show
  1. utils/general.py +6 -4
utils/general.py CHANGED
@@ -591,14 +591,16 @@ def apply_classifier(x, model, img, im0):
591
  return x
592
 
593
 
594
- def increment_path(path, exist_ok=True, sep=''):
595
- # Increment path, i.e. runs/exp --> runs/exp{sep}0, runs/exp{sep}1 etc.
596
  path = Path(path) # os-agnostic
597
- if (path.exists() and exist_ok) or (not path.exists()):
598
  return str(path)
599
  else:
 
 
600
  dirs = glob.glob(f"{path}{sep}*") # similar paths
601
  matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs]
602
  i = [int(m.groups()[0]) for m in matches if m] # indices
603
  n = max(i) + 1 if i else 2 # increment number
604
- return f"{path}{sep}{n}" # update path
 
591
  return x
592
 
593
 
594
+ def increment_path(path, exist_ok=False, sep=''):
595
+ # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.
596
  path = Path(path) # os-agnostic
597
+ if not path.exists() or exist_ok:
598
  return str(path)
599
  else:
600
+ suffix = path.suffix
601
+ path = path.with_suffix('')
602
  dirs = glob.glob(f"{path}{sep}*") # similar paths
603
  matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs]
604
  i = [int(m.groups()[0]) for m in matches if m] # indices
605
  n = max(i) + 1 if i else 2 # increment number
606
+ return f"{path}{sep}{n}{suffix}" # update path