Entry
How can I find the total size of a directory on my NT machine?
Jun 2nd, 2000 05:05
unknown unknown, Pieter Claerhout, Steve Tregidgo, Robert Roy
<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)
amount, units = getDirSize(dir)
print "Size: %s %s" % (amount, units)
if __name__ == '__main__':
main()
</code>
if getDirSize returns a 2-tuple you can simply do
print "Size: %s %s" %getDirSize(dir)
or, slice it if it's a longer tuple
print "Size: %s %s" %getDirSize(dir)[:2]