Is there a better way to get the root size instead of using os.walk?
import os
def get_size( start_path='.' ):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
print get_size("C:/")
I'm trying this code(which I got from here), it works fine inside folders, not that fast, but when I try it in the root directory it's super slow or sometimes it crashes [WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect]. Is there a way to get the root size just like left cliking properties in C:\?
EDIT: I tweaked a little bit the code to avoid the errors.
fp = os.path.join(dirpath, f)
try:
stat = os.stat(fp)
except OSError:
continue
try:
seen[stat.st_ino]
except KeyError:
seen[stat.st_ino] = True
else:
continue
total_size += stat.st_size
But it still slow as hell. It takes 6~7 minutes to calculate it.