1

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

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

谢谢

4

1 回答 1

2

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

要计算一个目录及其子目录使用的磁盘空间,请参阅:使用 Python 计算目录的大小?

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

于 2019-02-21T12:39:37.513 回答