【问题标题】:Bytes returned by "disk_usage" from the shutil library doesn't match the directory file sizeshutil 库中“disk_usage”返回的字节与目录文件大小不匹配
【发布时间】:2019-02-21 12:35:34
【问题描述】:

我正在从 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)

为什么我没有得到预期的输出?

谢谢

【问题讨论】:

    标签: python shutil disk-access


    【解决方案1】:

    shutil.disk_usage() 返回整个磁盘(文件系统、卷)的统计信息,而不仅仅是您传入的特定目录。

    要计算一个目录及其子目录使用的磁盘空间,请参阅:Calculating a directory's size using Python?

    有关一些潜在陷阱的详细介绍,请参阅:https://blogs.msdn.microsoft.com/oldnewthing/20041228-00/?p=36863

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-12
      • 2018-06-07
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2013-01-07
      相关资源
      最近更新 更多