简体   繁体   English

Python shutil.disk_usage 可用和已用空间值不反映实际已用和可用空间值

[英]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).我使用 Python 的 shutil.disk_usage function 使用“/”作为参数来确定我的计算机的总空间、已用空间和可用空间(我有一台使用 macOS Catalina 的 Mac)。 The results I got did not match up with the actual results.我得到的结果与实际结果不符。

Results from shutil.disk_usage("/"): shutil.disk_usage("/") 的结果:
total = 121.12 GB总计 = 121.12 GB
used = 11.3 GB已用 = 11.3 GB
free = 19.79 GB免费 = 19.79 GB

Actual results by inspecting my computer's storage:通过检查我的计算机存储的实际结果:
total = 121.12 GB总计 = 121.12 GB
used = 97.78 GB {Docs (35.34GB) + Apps (20.19GB) + System (11.3GB) + Other (30.95GB)}已用 = 97.78 GB {文档 (35.34GB) + 应用程序 (20.19GB) + 系统 (11.3GB) + 其他 (30.95GB)}
free = 22.64 GB免费 = 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:系统正在使用 11.3 GB,这是 shutil.disk_usage("/") 计算的确切数量,但是当我查看计算此数量的代码时,报告为:

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.我不明白为什么这个值报告为 11.3 GB,而不是已用空间的总量。

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".我将其读为“非超级用户可用的块数(我认为这意味着任何没有 sudo 访问权限的人都可以使用免费块,因此访客帐户可以使用免费块)乘以块大小等于免费的数量空间”。 I do not understand why this value is reported as 19.79 GB when I have 22.64 GB of free space.我不明白为什么当我有 22.64 GB 的可用空间时,这个值报告为 19.79 GB。

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.使用 shutil.disk_usage("/") 计算的已用和可用空间的值为何与实际值不同。

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.在搜索了一段时间后,我遇到了这篇文章Get hard disk size in Python ,其中评论指出 shutil.disk_usage 只给出分区的大小,而不是整个磁盘。

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)我使用 df -h 列出了所有文件系统及其挂载点和磁盘使用统计信息(总大小、已用空间、可用空间)

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)将 df -h 的结果与给定挂载点的 shutil.disk_usage 进行比较,将 df -h 中的值乘以 1024^3 字节/gb(macOS 使用 1000^3bytes/gb 报告它们的存储值)得到相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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