Skip to content

Reference for ultralytics/utils/files.py

Improvements

This page is sourced from https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/files.py. Have an improvement or example to add? Open a Pull Request — thank you! 🙏


class ultralytics.utils.files.WorkingDirectory

WorkingDirectory(self, new_dir: str | Path)

Bases: contextlib.ContextDecorator

A context manager and decorator for temporarily changing the working directory.

This class allows for the temporary change of the working directory using a context manager or decorator. It ensures that the original working directory is restored after the context or decorated function completes.

Args

NameTypeDescriptionDefault
new_dirstr | Pathrequired

Attributes

NameTypeDescription
dirPath | strThe new directory to switch to.
cwdPathThe original current working directory before the switch.

Methods

NameDescription
__enter__Change the current working directory to the specified directory upon entering the context.
__exit__Restore the original working directory when exiting the context.

Examples

Using as a context manager:
>>> with WorkingDirectory('/path/to/new/dir'):
>>> # Perform operations in the new directory
>>>     pass

Using as a decorator:
>>> @WorkingDirectory('/path/to/new/dir')
>>> def some_function():
>>> # Perform operations in the new directory
>>>     pass
Source code in ultralytics/utils/files.pyView on GitHub
class WorkingDirectory(contextlib.ContextDecorator):
    """A context manager and decorator for temporarily changing the working directory.

    This class allows for the temporary change of the working directory using a context manager or decorator. It ensures
    that the original working directory is restored after the context or decorated function completes.

    Attributes:
        dir (Path | str): The new directory to switch to.
        cwd (Path): The original current working directory before the switch.

    Methods:
        __enter__: Changes the current directory to the specified directory.
        __exit__: Restores the original working directory on context exit.

    Examples:
        Using as a context manager:
        >>> with WorkingDirectory('/path/to/new/dir'):
        >>> # Perform operations in the new directory
        >>>     pass

        Using as a decorator:
        >>> @WorkingDirectory('/path/to/new/dir')
        >>> def some_function():
        >>> # Perform operations in the new directory
        >>>     pass
    """

    def __init__(self, new_dir: str | Path):
        """Initialize the WorkingDirectory context manager with the target directory."""
        self.dir = new_dir  # new dir
        self.cwd = Path.cwd().resolve()  # current dir


method ultralytics.utils.files.WorkingDirectory.__enter__

def __enter__(self)

Change the current working directory to the specified directory upon entering the context.

Source code in ultralytics/utils/files.pyView on GitHub
def __enter__(self):
    """Change the current working directory to the specified directory upon entering the context."""
    os.chdir(self.dir)


method ultralytics.utils.files.WorkingDirectory.__exit__

def __exit__(self, exc_type, exc_val, exc_tb)

Restore the original working directory when exiting the context.

Args

NameTypeDescriptionDefault
exc_typerequired
exc_valrequired
exc_tbrequired
Source code in ultralytics/utils/files.pyView on GitHub
def __exit__(self, exc_type, exc_val, exc_tb):
    """Restore the original working directory when exiting the context."""
    os.chdir(self.cwd)





function ultralytics.utils.files.spaces_in_path

def spaces_in_path(path: str | Path)

Context manager to handle paths with spaces in their names.

If a path contains spaces, it replaces them with underscores, copies the file/directory to the new path, executes the context code block, then copies the file/directory back to its original location.

Args

NameTypeDescriptionDefault
pathstr | PathThe original path that may contain spaces.required

Examples

>>> with spaces_in_path('/path/with spaces') as new_path:
>>> # Your code here
>>>     pass

Yields

TypeDescription
Path | strTemporary path with any spaces replaced by underscores.
Source code in ultralytics/utils/files.pyView on GitHub
@contextmanager
def spaces_in_path(path: str | Path):
    """Context manager to handle paths with spaces in their names.

    If a path contains spaces, it replaces them with underscores, copies the file/directory to the new path, executes
    the context code block, then copies the file/directory back to its original location.

    Args:
        path (str | Path): The original path that may contain spaces.

    Yields:
        (Path | str): Temporary path with any spaces replaced by underscores.

    Examples:
        >>> with spaces_in_path('/path/with spaces') as new_path:
        >>> # Your code here
        >>>     pass
    """
    # If path has spaces, replace them with underscores
    if " " in str(path):
        string = isinstance(path, str)  # input type
        path = Path(path)

        # Create a temporary directory and construct the new path
        with tempfile.TemporaryDirectory() as tmp_dir:
            tmp_path = Path(tmp_dir) / path.name.replace(" ", "_")

            # Copy file/directory
            if path.is_dir():
                shutil.copytree(path, tmp_path)
            elif path.is_file():
                tmp_path.parent.mkdir(parents=True, exist_ok=True)
                shutil.copy2(path, tmp_path)

            try:
                # Yield the temporary path
                yield str(tmp_path) if string else tmp_path

            finally:
                # Copy file/directory back
                if tmp_path.is_dir():
                    shutil.copytree(tmp_path, path, dirs_exist_ok=True)
                elif tmp_path.is_file():
                    shutil.copy2(tmp_path, path)  # Copy back the file

    else:
        # If there are no spaces, just yield the original path
        yield path





function ultralytics.utils.files.increment_path

def increment_path(path: str | Path, exist_ok: bool = False, sep: str = "", mkdir: bool = False) -> Path

Increment a file or directory path, i.e., runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.

If the path exists and exist_ok is not True, the path will be incremented by appending a number and sep to the end of the path. If the path is a file, the file extension will be preserved. If the path is a directory, the number will be appended directly to the end of the path.

Args

NameTypeDescriptionDefault
pathstr | PathPath to increment.required
exist_okbool, optionalIf True, the path will not be incremented and returned as-is.False
sepstr, optionalSeparator to use between the path and the incrementation number.""
mkdirbool, optionalCreate a directory if it does not exist.False

Returns

TypeDescription
PathIncremented path.

Examples

Increment a directory path:
>>> from pathlib import Path
>>> path = Path("runs/exp")
>>> new_path = increment_path(path)
>>> print(new_path)
runs/exp2

Increment a file path:
>>> path = Path("runs/exp/results.txt")
>>> new_path = increment_path(path)
>>> print(new_path)
runs/exp/results2.txt
Source code in ultralytics/utils/files.pyView on GitHub
def increment_path(path: str | Path, exist_ok: bool = False, sep: str = "", mkdir: bool = False) -> Path:
    """Increment a file or directory path, i.e., runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.

    If the path exists and `exist_ok` is not True, the path will be incremented by appending a number and `sep` to the
    end of the path. If the path is a file, the file extension will be preserved. If the path is a directory, the number
    will be appended directly to the end of the path.

    Args:
        path (str | Path): Path to increment.
        exist_ok (bool, optional): If True, the path will not be incremented and returned as-is.
        sep (str, optional): Separator to use between the path and the incrementation number.
        mkdir (bool, optional): Create a directory if it does not exist.

    Returns:
        (Path): Incremented path.

    Examples:
        Increment a directory path:
        >>> from pathlib import Path
        >>> path = Path("runs/exp")
        >>> new_path = increment_path(path)
        >>> print(new_path)
        runs/exp2

        Increment a file path:
        >>> path = Path("runs/exp/results.txt")
        >>> new_path = increment_path(path)
        >>> print(new_path)
        runs/exp/results2.txt
    """
    path = Path(path)  # os-agnostic
    if path.exists() and not exist_ok:
        path, suffix = (path.with_suffix(""), path.suffix) if path.is_file() else (path, "")

        # Method 1
        for n in range(2, 9999):
            p = f"{path}{sep}{n}{suffix}"  # increment path
            if not os.path.exists(p):
                break
        path = Path(p)

    if mkdir:
        path.mkdir(parents=True, exist_ok=True)  # make directory

    return path





function ultralytics.utils.files.file_age

def file_age(path: str | Path = __file__) -> int

Return days since the last modification of the specified file.

Args

NameTypeDescriptionDefault
pathstr | Path__file__
Source code in ultralytics/utils/files.pyView on GitHub
def file_age(path: str | Path = __file__) -> int:
    """Return days since the last modification of the specified file."""
    dt = datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)  # delta
    return dt.days  # + dt.seconds / 86400  # fractional days





function ultralytics.utils.files.file_date

def file_date(path: str | Path = __file__) -> str

Return the file modification date in 'YYYY-M-D' format.

Args

NameTypeDescriptionDefault
pathstr | Path__file__
Source code in ultralytics/utils/files.pyView on GitHub
def file_date(path: str | Path = __file__) -> str:
    """Return the file modification date in 'YYYY-M-D' format."""
    t = datetime.fromtimestamp(Path(path).stat().st_mtime)
    return f"{t.year}-{t.month}-{t.day}"





function ultralytics.utils.files.file_size

def file_size(path: str | Path) -> float

Return the size of a file or directory in megabytes (MB).

Args

NameTypeDescriptionDefault
pathstr | Pathrequired
Source code in ultralytics/utils/files.pyView on GitHub
def file_size(path: str | Path) -> float:
    """Return the size of a file or directory in megabytes (MB)."""
    if isinstance(path, (str, Path)):
        mb = 1 << 20  # bytes to MiB (1024 ** 2)
        path = Path(path)
        if path.is_file():
            return path.stat().st_size / mb
        elif path.is_dir():
            return sum(f.stat().st_size for f in path.glob("**/*") if f.is_file()) / mb
    return 0.0





function ultralytics.utils.files.get_latest_run

def get_latest_run(search_dir: str = ".") -> str

Return the path to the most recent 'last.pt' file in the specified directory for resuming training.

Args

NameTypeDescriptionDefault
search_dirstr"."
Source code in ultralytics/utils/files.pyView on GitHub
def get_latest_run(search_dir: str = ".") -> str:
    """Return the path to the most recent 'last.pt' file in the specified directory for resuming training."""
    last_list = glob.glob(f"{search_dir}/**/last*.pt", recursive=True)
    return max(last_list, key=os.path.getctime) if last_list else ""





function ultralytics.utils.files.update_models

def update_models(model_names: tuple = ("yolo11n.pt",), source_dir: Path = Path("."), update_names: bool = False)

Update and re-save specified YOLO models in an 'updated_models' subdirectory.

Args

NameTypeDescriptionDefault
model_namestuple, optionalModel filenames to update.("yolo11n.pt",)
source_dirPath, optionalDirectory containing models and target subdirectory.Path(".")
update_namesbool, optionalUpdate model names from a data YAML.False

Examples

Update specified YOLO models and save them in 'updated_models' subdirectory:
>>> from ultralytics.utils.files import update_models
>>> model_names = ("yolo11n.pt", "yolov8s.pt")
>>> update_models(model_names, source_dir=Path("/models"), update_names=True)
Source code in ultralytics/utils/files.pyView on GitHub
def update_models(model_names: tuple = ("yolo11n.pt",), source_dir: Path = Path("."), update_names: bool = False):
    """Update and re-save specified YOLO models in an 'updated_models' subdirectory.

    Args:
        model_names (tuple, optional): Model filenames to update.
        source_dir (Path, optional): Directory containing models and target subdirectory.
        update_names (bool, optional): Update model names from a data YAML.

    Examples:
        Update specified YOLO models and save them in 'updated_models' subdirectory:
        >>> from ultralytics.utils.files import update_models
        >>> model_names = ("yolo11n.pt", "yolov8s.pt")
        >>> update_models(model_names, source_dir=Path("/models"), update_names=True)
    """
    from ultralytics import YOLO
    from ultralytics.nn.autobackend import default_class_names

    target_dir = source_dir / "updated_models"
    target_dir.mkdir(parents=True, exist_ok=True)  # Ensure target directory exists

    for model_name in model_names:
        model_path = source_dir / model_name
        print(f"Loading model from {model_path}")

        # Load model
        model = YOLO(model_path)
        model.half()
        if update_names:  # update model names from a dataset YAML
            model.model.names = default_class_names("coco8.yaml")

        # Define new save path
        save_path = target_dir / model_name

        # Save model using model.save()
        print(f"Re-saving {model_name} model to {save_path}")
        model.save(save_path)





📅 Created 2 years ago ✏️ Updated 2 days ago
glenn-jocherjk4eBurhan-Q