Reference for ultralytics/utils/files.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/files.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.files.WorkingDirectory
Bases: 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:
Name | Type | Description |
---|---|---|
dir |
Path
|
The new directory to switch to. |
cwd |
Path
|
The original current working directory before the switch. |
Methods:
Name | Description |
---|---|
__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
Source code in ultralytics/utils/files.py
__enter__
__exit__
ultralytics.utils.files.spaces_in_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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
The original path that may contain spaces. |
required |
Yields:
Type | Description |
---|---|
Path
|
Temporary path with spaces replaced by underscores if spaces were present, otherwise the original path. |
Examples:
Use the context manager to handle paths with spaces:
>>> from ultralytics.utils.files import spaces_in_path
>>> with spaces_in_path('/path/with spaces') as new_path:
>>> # Your code here
Source code in ultralytics/utils/files.py
ultralytics.utils.files.increment_path
Increments 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. If mkdir
is set to True, the path will be created as a
directory if it does not already exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
Path to increment. |
required |
exist_ok
|
bool
|
If True, the path will not be incremented and returned as-is. |
False
|
sep
|
str
|
Separator to use between the path and the incrementation number. |
''
|
mkdir
|
bool
|
Create a directory if it does not exist. |
False
|
Returns:
Type | Description |
---|---|
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
Source code in ultralytics/utils/files.py
ultralytics.utils.files.file_age
Return days since the last modification of the specified file.
ultralytics.utils.files.file_date
ultralytics.utils.files.file_size
Returns the size of a file or directory in megabytes (MB).
Source code in ultralytics/utils/files.py
ultralytics.utils.files.get_latest_run
Returns the path to the most recent 'last.pt' file in the specified directory for resuming training.
Source code in ultralytics/utils/files.py
ultralytics.utils.files.update_models
Updates and re-saves specified YOLO models in an 'updated_models' subdirectory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_names
|
Tuple[str, ...]
|
Model filenames to update. |
('yolo11n.pt')
|
source_dir
|
Path
|
Directory containing models and target subdirectory. |
Path('.')
|
update_names
|
bool
|
Update 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)