Disk
provide.foundation.file.disk
¶
Disk space and filesystem utilities.
Provides functions for checking available disk space before performing operations that may require significant storage.
Functions¶
check_disk_space
¶
Check if sufficient disk space is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Directory path to check (or parent if it doesn't exist) |
required |
required_bytes
|
int
|
Number of bytes required |
required |
raise_on_insufficient
|
bool
|
Raise OSError if insufficient space (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if sufficient space available, False otherwise |
Raises:
| Type | Description |
|---|---|
OSError
|
If insufficient space and raise_on_insufficient=True |
Examples:
>>> from pathlib import Path
>>> # Check if 1GB is available
>>> check_disk_space(Path.home(), 1024**3, raise_on_insufficient=False)
True
>>> # Will raise if insufficient (default behavior)
>>> check_disk_space(Path.home(), 10**15)
Traceback (most recent call last):
...
OSError: Insufficient disk space...
Notes
On systems where disk space cannot be determined (e.g., Windows without proper permissions), this function logs a warning but does not fail, returning True to allow the operation to proceed.
Source code in provide/foundation/file/disk.py
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 106 107 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
format_bytes
¶
Format bytes as human-readable string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_bytes
|
int
|
Number of bytes |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string (e.g., "1.50 GB", "256.00 MB") |
Examples:
>>> format_bytes(1024)
'1.00 KB'
>>> format_bytes(1024**2)
'1.00 MB'
>>> format_bytes(1536 * 1024**2)
'1.50 GB'
>>> format_bytes(500)
'500 B'
Source code in provide/foundation/file/disk.py
get_available_space
¶
Get available disk space in bytes for a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Directory path to check (uses parent if path doesn't exist) |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Available bytes or None if unable to determine |
Examples:
>>> from pathlib import Path
>>> space = get_available_space(Path.home())
>>> space is not None and space > 0
True
Notes
Uses os.statvfs on Unix-like systems (Linux, macOS, BSD). Returns None on Windows or if statvfs is unavailable.
Source code in provide/foundation/file/disk.py
get_disk_usage
¶
Get total, used, and free disk space for a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Directory path to check |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int] | None
|
Tuple of (total, used, free) in bytes, or None if unable to determine |
Examples:
>>> from pathlib import Path
>>> usage = get_disk_usage(Path.home())
>>> if usage:
... total, used, free = usage
... assert total > 0 and used >= 0 and free > 0
... assert total >= used + free # May have reserved space
Notes
Uses os.statvfs on Unix-like systems. Returns None on Windows or if unavailable.