files_to_free_space
files_to_free_space(root, target_bytes)Determine the minimum number of files that would need to be deleted to free at least a given amount of disk space.
This function DOES NOT delete anything.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| root | Path | Root directory to scan. | required |
| target_bytes | int | Desired amount of space to free, in bytes. | required |
Returns
| Name | Type | Description |
|---|---|---|
| list[FileInfo] | A list of files whose combined size is greater than or equal to target_bytes. Files are selected greedily, starting from the largest. |
Notes
- Files are selected by descending size.
- Selection stops as soon as the target is met.
- If target_bytes <= 0, an empty list is returned.
- If total file size is insufficient, all files are returned.
- No files are deleted.
Examples
>>> import tempfile
>>> from pathlib import Path
>>> with tempfile.TemporaryDirectory() as tmp:
... root = Path(tmp)
... _ = (root / "a.txt").write_text("a")
... _ = (root / "b.txt").write_text("bbb")
... [item.path.name for item in files_to_free_space(root, 2)]
['b.txt']