To get the free disk space of a system in Python, you can use the psutil
module.
Below is an example code snippet that demonstrates how to get the free disk space in bytes of the root directory of the system:
main.py109 chars6 lines
This script imports the psutil
module and retrieves the disk usage of the root directory (/
) using the psutil.disk_usage()
method. This returns a namedtuple
with properties total
, used
, and free
, representing the total disk space, disk space in use, and free disk space, respectively.
We then extract the value of free
and print it, along with the unit byte, to the console.
gistlibby LogSnag