Reference for ultralytics/utils/files.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/files.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.files.WorkingDirectory
Bases: ContextDecorator
Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager.
Source code in ultralytics/utils/files.py
__enter__()
__exit__(exc_type, exc_val, exc_tb)
__init__(new_dir)
ultralytics.utils.files.spaces_in_path(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. |
required |
Yields:
Type | Description |
---|---|
Path
|
Temporary path with spaces replaced by underscores if spaces were present, otherwise the original path. |
Example
Source code in ultralytics/utils/files.py
ultralytics.utils.files.increment_path(path, exist_ok=False, sep='', mkdir=False)
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 set to 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. Defaults to False. |
False
|
sep |
str
|
Separator to use between the path and the incrementation number. Defaults to ''. |
''
|
mkdir |
bool
|
Create a directory if it does not exist. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Path
|
Incremented path. |
Source code in ultralytics/utils/files.py
ultralytics.utils.files.file_age(path=__file__)
Return days since last file update.
ultralytics.utils.files.file_date(path=__file__)
Return human-readable file modification date, i.e. '2021-3-26'.
ultralytics.utils.files.file_size(path)
Return file/dir size (MB).
Source code in ultralytics/utils/files.py
ultralytics.utils.files.get_latest_run(search_dir='.')
Return path to most recent 'last.pt' in /runs (i.e. to --resume from).