#!/usr/bin/env python # # Well this is a bloody kickass little app which tells you things which could # really be obtained elsewhere, but what are you going to do 'eh! It walks a # directory structure and collects the number of files, as well as their size, # then prints whichever data you like. bla bla bla! The root directory will be # the current working diectory unless you specify another location with the -d # switch (good idea). One important thing to note is that data is easy to read # because it will tell you what the numbers are for, except when using the -r # switch which just spits the numbers out seperated by a space. In this case # the numbers are in the same order. # import getopt import sys import os import datetime # Settings. ShowTimestamp = False ShowDirCount = False ShowFileCount = False ShowAverageFileSize = False ShowTotalFileSize = False RootDir = os.getcwd() ShowRaw = False def Traverse(Path): # Working values. global FileCount global DirCount global TotalFileSize # Get all children. try: Children = os.listdir(Path) except OSError, (errno, strerror): return for Child in Children: if os.path.isdir(Path + '/' + Child): DirCount += 1 Traverse(Path + '/' + Child) elif os.path.isfile(Path + '/' + Child): FileCount += 1 TotalFileSize += os.path.getsize(Path + '/' + Child) def GetTimestamp(): if ShowTimestamp: return datetime.datetime.now() else: return None def GetDirCount(): if ShowDirCount: return DirCount else: return None def GetFileCount(): if ShowFileCount: return FileCount else: return None def GetAverageFileSize(): if ShowAverageFileSize: return TotalFileSize / FileCount else: return None def GetTotalFileSize(): if ShowTotalFileSize: return TotalFileSize else: return None def Usage(): print 'some day this app will be cool enough to have a usage description!' # Set initial global values. FileCount = 0 DirCount = 0 TotalFileSize = 0 # Get commandline parameters and make sure they are valid. try: opts, args = getopt.getopt(sys.argv[1:], "d:tcfraoah", ["dir=", "timestamp", "circount", "filecount", "raw", "avgfilesize", "totalfilesize", "all", "help"]) except getopt.GetoptError: Usage() sys.exit() # Set application options. for o, a in opts: if o in ("-d", "--dir"): RootDir = a if o in ("-t", "--timestamp"): ShowTimestamp = True if o in ("-c", "--dircount"): ShowDirCount = True if o in ("-f", "--filecount"): ShowFileCount = True if o in ("-r", "--raw"): ShowRaw = True if o in ("-a", "--avgfilesize"): ShowAverageFileSize = True if o in ("-o", "--totalfilesize"): ShowTotalFileSize = True if o in ("-a", "--all"): ShowTimestamp = True ShowDirCount = True ShowFileCount = True ShowAverageFileSize = True ShowTotalFileSize = True if o in ("-h", "--help"): Usage() sys.exit() # Must not have trailing slash! Traverse(RootDir) def PrintResults(): if ShowRaw: print "%s %s %s %s %s" % (GetTimestamp(), GetDirCount(), GetFileCount(), GetAverageFileSize(), GetTotalFileSize()) else: print """Timestamp = %s Directory Count = %s File Count = %s Average File Size = %s Total File Size = %s""" % (GetTimestamp(), GetDirCount(), GetFileCount(), GetAverageFileSize(), GetTotalFileSize()) PrintResults()