File size: 1,235 Bytes
e1832f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from pathlib import Path
import os

def increment_path(path, exist_ok=False, sep="", mkdir=False):
    """
    Generates an incremented file or directory path if it already exists, with an option to create the directory.

    Args:
        path (str or Path): Initial file or directory path.
        exist_ok (bool): If True, returns the original path even if it exists.
        sep (str): Separator to use between path stem and increment.
        mkdir (bool): If True, creates the directory if it doesn’t exist.

    Returns:
        Path: Incremented path, or original if exist_ok is True.
        
    Example:
        runs/exp --> runs/exp2, runs/exp3, etc.
    """
    path = Path(path)  # ensures OS compatibility
    if path.exists() and not exist_ok:
        base, suffix = (path.with_suffix(""), path.suffix) if path.is_file() else (path, "")
        
        # Increment path until a non-existing one is found
        for n in range(2, 9999):
            new_path = f"{base}{sep}{n}{suffix}"
            if not Path(new_path).exists():
                path = Path(new_path)
                break

    if mkdir:
        path.mkdir(parents=True, exist_ok=True)  # creates the directory if it doesn’t exist

    return path