← Go Back
On Windows:
On Linux:
Then you can fetch the usage data (expressed in bytes) with
Remember to install psutil first by running:
How to Get Used/Free/Total Disk Space
Using thedisk_usage()
function from the psutil library.On Windows:
import psutil
disk_usage = psutil.disk_usage("C:\\")
On Linux:
import psutil
disk_usage = psutil.disk_usage("/")
Then you can fetch the usage data (expressed in bytes) with
disk_usage.used
, disk_usage.free
and disk_usage.total
:def to_gb(bytes):
"""Convert bytes to gigabytes."""
return bytes / 1024**3
print("Total space: {:.2f} GB.".format(to_gb(disk_usage.total)))
print("Free space: {:.2f} GB.".format(to_gb(disk_usage.free)))
print("Used space: {:.2f} GB.".format(to_gb(disk_usage.used)))
print("Percentage of used space: {}%.".format(disk_usage.percent))
Remember to install psutil first by running:
pip install psutil