22 lines
857 B
Python
22 lines
857 B
Python
from pathlib import Path
|
||
import glob
|
||
import re
|
||
|
||
|
||
# 保存路径管理
|
||
def increment_path(path, exist_ok=False, sep='', mkdir=False):
|
||
# 引用:https://github.com/ultralytics/yolov5/blob/master/utils/general.py
|
||
path = Path(path) # os-agnostic
|
||
if path.exists() and not exist_ok:
|
||
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
|
||
path = Path(f"{path}{sep}{n}{suffix}") # update path
|
||
dir = path if path.suffix == '' else path.parent # directory
|
||
if not dir.exists() and mkdir:
|
||
dir.mkdir(parents=True, exist_ok=True) # make directory
|
||
return path
|