Python 3.6.0、Windows 10(64 位)
import os
path = r'C:\Windows\System32\AdvancedInstallers\cmiv2.dll'
print(os.path.getsize(path))
这段代码给出了 2193248
的输出,但是当我看到这个文件的属性时,它显示了 3252576
。它不是磁盘属性的大小。我说的是实际尺寸。
我期待 python 程序打印 3252576
。
最佳答案
可能与NTFS alternate data streams有关.尽管文章指出文件中 ADS 的存在不会影响其大小,但自文章撰写以来(早在 2004 年)情况可能发生了变化,Windows 10 的属性对话框很可能会说明文件中的所有 ADS,而 python没有。
A more recent blog post说明如何使用 PowerShell 实用程序检查文件的数据流:
Get-item -Path C:\Windows\System32\AdvancedInstallers\cmiv2.dll -stream *
如果我的假设是正确的,那么输出应该如下所示:
FileName: C:\Windows\System32\AdvancedInstallers\cmiv2.dll
Stream Length
------ ------
:$DATA 2193248
SomethingElse 1059328
(或者 SomethingElse 实际上可能由几个较小的流组成,大小总和为 1059328)。
关于python - os.path.getsize 在 Windows 10 中给出错误的 .dll 文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42335950/