get system free disk space in python

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.py
import psutil

disk = psutil.disk_usage('/')
free_space = disk.free
print(f'Free space: {free_space} bytes')
109 chars
6 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.

related categories

gistlibby LogSnag