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
WorkingDirectory(new_dir: Union[str, Path])
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 | str
|
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
42 43 44 45 |
|
__enter__
__enter__()
Change the current working directory to the specified directory upon entering the context.
Source code in ultralytics/utils/files.py
47 48 49 |
|
__exit__
__exit__(exc_type, exc_val, exc_tb)
Restore the original working directory when exiting the context.
Source code in ultralytics/utils/files.py
51 52 53 |
|
ultralytics.utils.files.spaces_in_path
spaces_in_path(path: Union[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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path
|
The original path that may contain spaces. |
required |
Yields:
Type | Description |
---|---|
Path | str
|
Temporary path with spaces replaced by underscores if spaces were present, otherwise the original path. |
Examples:
>>> with spaces_in_path('/path/with spaces') as new_path:
>>> # Your code here
>>> pass
Source code in ultralytics/utils/files.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
ultralytics.utils.files.increment_path
increment_path(
path: Union[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.
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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
ultralytics.utils.files.file_age
file_age(path: Union[str, Path] = __file__) -> int
Return days since the last modification of the specified file.
Source code in ultralytics/utils/files.py
156 157 158 159 |
|
ultralytics.utils.files.file_date
file_date(path: Union[str, Path] = __file__) -> str
Return the file modification date in 'YYYY-M-D' format.
Source code in ultralytics/utils/files.py
162 163 164 165 |
|
ultralytics.utils.files.file_size
file_size(path: Union[str, Path]) -> float
Return the size of a file or directory in megabytes (MB).
Source code in ultralytics/utils/files.py
168 169 170 171 172 173 174 175 176 177 |
|
ultralytics.utils.files.get_latest_run
get_latest_run(search_dir: str = '.') -> str
Return the path to the most recent 'last.pt' file in the specified directory for resuming training.
Source code in ultralytics/utils/files.py
180 181 182 183 |
|
ultralytics.utils.files.update_models
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_names
|
tuple
|
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)
Source code in ultralytics/utils/files.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|