<p>我正在寻找一种快速的方法来计算Windows上Python中文件夹的大小。这就是我目前所拥有的:</p>
<pre><code>def get_dir_size(path):
total_size = 0
if platform.system() == 'Windows':
try:
items = win32file.FindFilesW(path + '\\*')
except Exception, err:
return 0
# Add the size or perform recursion on folders.
for item in items:
attr = item[0]
name = item[-2]
size = item[5]
if (attr & win32con.FILE_ATTRIBUTE_DIRECTORY) and \
not (attr & win32con.FILE_ATTRIBUTE_SYSTEM): # skip system dirs
if name not in DIR_EXCLUDES:
total_size += get_dir_size("%s\\%s" % (path, name))
total_size += size
return total_size
</code></pre>
<p>当文件夹大小超过100克时,这还不够好。有什么改进的办法吗?</p>
<p>在一台高速机器(2Ghz+-5G内存)上,226001个文件和12043个文件夹中的422GB需要72秒。使用资源管理器属性选项需要40秒。</p>
<p>我知道我有点贪心,但我希望有更好的解决办法。</p>
<p>洛朗·卢斯</p>