简体   繁体   中英

Python shutil.disk_usage free and used space values do not reflect actual used and free space values

I used Python's shutil.disk_usage function using "/" as the parameter to determine my computer's total, used, and free space (I have a Mac using macOS Catalina). The results I got did not match up with the actual results.

Results from shutil.disk_usage("/"):
total = 121.12 GB
used = 11.3 GB
free = 19.79 GB

Actual results by inspecting my computer's storage:
total = 121.12 GB
used = 97.78 GB {Docs (35.34GB) + Apps (20.19GB) + System (11.3GB) + Other (30.95GB)}
free = 22.64 GB

The total value is correct for both, but the used and free space values are off.

The system is using 11.3 GB, which is the exact amount that shutil.disk_usage("/") calculated, but when I look at the code for calculating this amount it is reported as:

used = (f_blocks - f_bfree) * f_frsize

Which I read as "total blocks minus free blocks (used blocks) times block size is equal to the amount of used space". I do not understand why this value is reported as 11.3 GB, and not the total amount of used space.

Similarly I do not understand why the value for free space:

free = f_bavail * f_frsize

Which I read as "number of blocks available to non-super user (I think this means free blocks available to anyone who does not have sudo access, so free blocks available to the guest account) times block size is equal to the amount of free space". I do not understand why this value is reported as 19.79 GB when I have 22.64 GB of free space.

I expected that total = used + free but this is clearly not the case.

How come the calculated values for used and free space using shutil.disk_usage("/") are different from the actual values.

After searching for a while I came across this post Get hard disk size in Python where a comment states that shutil.disk_usage only gives the size of the partition and not the whole disk.

I used df -h to give a list of all file systems with their mount points and disk usage statistics (total size, used space, free space)

Comparing the results with df -h to shutil.disk_usage for a given mount point gave the same results when multiplying the values from df -h by 1024^3 bytes/gb (macOS reports their storage values using 1000^3bytes/gb)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM