【问题标题】:Measure directory size with Python使用 Python 测量目录大小
【发布时间】:2017-05-04 11:27:48
【问题描述】:

我是 Python 新手,我需要从包含超过 500k 个文件的目录中获取大小。我在互联网上找到了一些应该非常快的代码。不知何故它不起作用,我不知道为什么。 当前输出只是“测试”,所以它甚至没有进入函数。

import time
import os

start_time = time.time()
print('Test')
def getSize(path):
    print('Test2')
    total = 0
    for entry in os.scandir(path):
        if entry.is_dir(follow_symlinks=False):
            total += getSize(entry.path)
        else:
            total += entry.stat(follow_symlinks=False).st_size
    return total

    print (float(getSize('U:\Java'))/1024/1024/1024)
    print("--- %s Sekunden ---" % round(time.time() - start_time, 2))

附加问题:有没有更快的方法来使用 python 或其他语言搜索如此大的目录?

【问题讨论】:

标签: python scandir


【解决方案1】:

程序中调用函数的最后两行是缩进的,因此被视为函数的一部分,不会执行。 只需将它们磨去即可。

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多