`increment_path()` robustness improvements (#7628)
Improved robustness to filename edge cases like `data/images/zidane.23.jpg` that currently fail. May resolve https://github.com/ultralytics/yolov5/issues/7432
This commit is contained in:
parent
177da7f348
commit
1a3ecb8b38
|
|
@ -933,13 +933,24 @@ def increment_path(path, exist_ok=False, sep='', mkdir=False):
|
||||||
path = Path(path) # os-agnostic
|
path = Path(path) # os-agnostic
|
||||||
if path.exists() and not exist_ok:
|
if path.exists() and not exist_ok:
|
||||||
path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '')
|
path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '')
|
||||||
dirs = glob.glob(f"{path}{sep}*") # similar paths
|
|
||||||
matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs]
|
# Method 1
|
||||||
i = [int(m.groups()[0]) for m in matches if m] # indices
|
for n in range(2, 9999):
|
||||||
n = max(i) + 1 if i else 2 # increment number
|
p = f'{path}{sep}{n}{suffix}' # increment path
|
||||||
path = Path(f"{path}{sep}{n}{suffix}") # increment path
|
if not os.path.exists(p): #
|
||||||
|
break
|
||||||
|
path = Path(p)
|
||||||
|
|
||||||
|
# Method 2 (deprecated)
|
||||||
|
# dirs = glob.glob(f"{path}{sep}*") # similar paths
|
||||||
|
# matches = [re.search(rf"{path.stem}{sep}(\d+)", 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
|
||||||
|
# path = Path(f"{path}{sep}{n}{suffix}") # increment path
|
||||||
|
|
||||||
if mkdir:
|
if mkdir:
|
||||||
path.mkdir(parents=True, exist_ok=True) # make directory
|
path.mkdir(parents=True, exist_ok=True) # make directory
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue