def getSize(path):
start_time = time.time()
totalSize = 0
if os.path.isdir(path):
for dirpath, dirnames, filenames in os.walk(path):
for fName in filenames:
fp = os.path.join(dirpath, fName)
totalSize += os.path.getsize(fp)
print time.time() - start_time, "seconds"
return totalSize
else:
return os.path.getsize(path)
上面的函数大约需要 25 秒才能找到现在包含大量文件的目录的大小。谁能告诉我一些有效的功能来做同样的事情,这样找到尺寸的时间会更短吗?