get system total disk space in python

To get the total disk space of a system in python, we can make use of the psutil library which can be installed using pip.

Here's the code to get the total disk space in bytes:

main.py
import psutil

total_disk_space = psutil.disk_usage('/').total
print("Total disk space (in bytes):", total_disk_space)
119 chars
5 lines

The above code uses the disk_usage method from the psutil module by passing the file system path as '/'. This method returns a named tuple representing the disk usage statistics for the path. We then extract the total space using the total attribute of the tuple.

This will output the total disk space in bytes. If you want to convert it to a different unit such as GB or TB, you can do so by dividing the total disk space with the corresponding unit-size in bytes.

gistlibby LogSnag