我正在从Python网站阅读shutil的官方文档,然后我运行了disk_usage测试,但它没有返回我所期望的内容,在里面目录(文件夹)中有一个669 kb的文件。
这是代码:
import os
import shutil
os.chdir(r"D:\python\topics\shutil\disk_usage")
directory = "test_folder"
total, used, free = shutil.disk_usage(directory)
print(used)
输出:
177422868480 (which I suppose is the value in bytes)
预期输出:
669000 (since the file inside is 669 kb)
为什么我没有得到预期的输出?
谢谢
最佳答案
shutil.disk_usage()
返回整个磁盘(文件系统、卷)的统计信息,而不仅仅是您传入的特定目录的统计信息。
要计算一个目录及其子目录使用的磁盘空间,请参阅:Calculating a directory's size using Python?
有关一些潜在陷阱的详细介绍,请参阅:https://blogs.msdn.microsoft.com/oldnewthing/20041228-00/?p=36863
关于python - "disk_usage"从shutil库返回的字节与目录文件大小不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807231/