#dirstats.py
Language:
PythonWritten by
doug on 2008-01-29 23:08:27
import os
import sys
verbose = False
human = True
def setdefaults():
global verbose, human
verbose = False
human = True
def help():
print "Not enough args, please supply at least one base directory"
print "fsstats: [-vh] dir[ dir dir dir...]"
print "-v turn verbose on"
print "-h turn human readable format off"
if len(sys.argv) < 2:
help()
sys.exit(1)
setdefaults()
first = 1
if sys.argv[1][0] == "-":
first = 2
for c in sys.argv[1][1:]:
if c == "v":
verbose = True
elif c == "h":
human = False
else: setdefaults() break
def humansize(nbytes):
if not human: return "%db" % (nbytes)
nbytes = float(nbytes)
outb = 0.0
if nbytes > 0x40000000: outb = "%fGB" % (nbytes / 0x40000000)
elif nbytes > 0x100000: outb = "%fMB" % (nbytes / 0x100000)
elif nbytes > 0x400: outb = "%fKB" % (nbytes / 0x400)
return outb
mostfiles = {"path": "", "number": 0}
totalnumdirs = 0
totalnumfiles = 0
totalsize = 0
biggestfile = {"path": "", "size": 0}
if len(sys.argv) < first+1:
help()
sys.exit(1)
for dir in sys.argv[first:]:
for dirpath, dirnames, filenames in os.walk(dir):
if verbose: print "Handling directory %s" % (dirpath)
if mostfiles["number"] < len(filenames):
mostfiles["number"] = len(filenames)
mostfiles["path"] = dirpath
totalnumdirs = totalnumdirs + len(dirnames)
totalnumfiles = totalnumfiles + len(filenames)
for file in filenames:
if not file: continue;
size = os.path.getsize("%s/%s" % (dirpath, file))
totalsize = totalsize + size
if biggestfile["size"] < size:
biggestfile["size"] = size
biggestfile["path"] = "%s/%s" % (dirpath, file)
print totalnumdirs, "total directories"
print totalnumfiles, "total files"
print humansize(totalsize), "total size"
print "%s (%s) was the biggest file" % (biggestfile["path"], humansize(biggestfile["size"]))
print "%s had the most files in it. %d files." % (mostfiles["path"], mostfiles["number"])
if (totalnumfiles):
print "There was an average of %f files in each directory" % (totalnumfiles / totalnumfiles)
print "There was an average filesize of %s" % humansize(totalsize / totalnumfiles)