Check Disk Space in Python

To check the available space on a disk, you can use the shutil module in Python to obtain the total space and available space on the disk. Below is a simple example code:

import shutil

total, used, free = shutil.disk_usage("/")

print("Total disk space:", total // (2**30), "GB")
print("Used disk space:", used // (2**30), "GB")
print("Free disk space:", free // (2**30), "GB")

In the code above, the shutil.disk_usage() function takes a path as a parameter and returns a tuple containing total space, used space, and available space. We divide these values by 2**30 to convert bytes to GB.

Please note that the path “/” represents the root directory, and you can change the path as needed to check the available space on other disks or directories.

bannerAds