Getting the directory size
Pieter Claerhout
PClaerhout at CREO.BE
Thu Jun 1 08:02:32 EDT 2000
Hi all,
I want to be able to find the total size of a directory on my NT machine,
and I started with writing it using the os.path.walk function, and it looks
something like this:
<code>
import os
import sys
def calcDirSize(arg, dir, files):
for file in files:
stats = os.stat(os.path.join(dir, file))
size = stats[6]
arg.append(size)
def getDirSize(dir):
sizes = []
os.path.walk(dir, calcDirSize, sizes)
total = 0
for size in sizes:
total = total + size
if total > 1073741824:
return (round(total/1073741824.0, 2), 'GB')
if total > 1048576:
return (round(total/1048576.0, 2), 'MB')
if total > 1024:
return (round(total/1024.0, 2), 'KB')
return (total, 'bytes')
def main():
dir = sys.argv[1]
print "Testing directorySize..."
print "Directory: %s" %(dir)
print "Size: %s %s" %(getDirSize(dir)[0], getDirSize(dir)[1])
if __name__ == '__main__':
main()
</code>
Is there any way to fasten up this beauty, because depending on the
number of directories it has to walk through, it takes a long time. Is
there maybe a function in the win32 modules who does this for me,
but lots faster?
Kind regards,
Pieter Claerhout
More information about the Python-list
mailing list