- 1 year ago
Get disk size minus reserved space
To get the disk size minus the reserved space in Windows using Python, you can use the psutil
library. Here's an example:
pythonimport psutil
def get_disk_size_without_reserved(path):
disk_usage = psutil.disk_usage(path)
reserved_space = disk_usage.total - disk_usage.free - disk_usage.used
disk_size_without_reserved = disk_usage.total - reserved_space
return disk_size_without_reserved
# Example usage
disk_size = get_disk_size_without_reserved("C:\\")
print(f"Disk Size without Reserved Space: {disk_size} bytes")
In the code above, the get_disk_size_without_reserved
function takes a path
parameter representing the disk or directory path for which you want to calculate the disk size without the reserved space. It uses the disk_usage
function from psutil
to retrieve disk usage information for the specified path. Then, it calculates the reserved space by subtracting the free and used space from the total space. Finally, it calculates the disk size without the reserved space by subtracting the reserved space from the total disk size.
You can pass the appropriate disk or directory path to the get_disk_size_without_reserved
function to obtain the disk size without the reserved space. In the example, it calculates the size for the "C:" drive, but you can modify it according to your requirements.
Note that the psutil
library needs to be installed (pip install psutil
) to use the disk_usage
function.