Python shutil.disk_usage
Inspect the capacity of the volume containing a directory.
A simple example
shutil.disk_usage
- Start with Example volume with 10 units total and 4 available
- Do this Compare a 3-unit request with available space
- Result It fits
Syntax
shutil.disk_usage(base)
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyCheck room for a class recording
A teacher wants to record a lesson on a chosen local volume. Query capacity for the directory that will hold the recording so a large system drive cannot mask a nearly full destination.
from pathlib import Path
from tempfile import TemporaryDirectory
import shutil
with TemporaryDirectory() as folder:
base = Path(folder)
if shutil.disk_usage(base).total > 0:
print("measured")
measured
The queried volume reports a positive total capacity.
Prepare a gallery label
A curator stores a short UTF-8 caption beside an exhibit. On a drive with room to spare, compare its available byte count with the size of one extra copy. Can this copy fit?
Prepare a studio handover
A stage manager saves a two-word instruction for the next shift. On a drive with room to spare, compare its available byte count with the size of one extra copy. Can this copy fit?
What to watch for
Space figures can change while the application runs and may be limited by account quotas.