#!/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 # Improved settings. Settings = { 'RootDir':['Root Directory', False, os.getcwd()], 'Timestamp':['Timestamp', False, 0], 'DirCount':['Directory Count', False, 0], 'FileCount':['File Count', False, 0], 'AvgFileSize':['Average File Bytes', False, 0], 'TotalFileSize':['Total File Bytes', False, 0] } # Settings. ShowRaw = False # 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"): Settings['RootDir'] = a if o in ("-t", "--timestamp"): Settings['Timestamp'][1] = True if o in ("-c", "--dircount"): Settings['DirCount'][1] = True if o in ("-f", "--filecount"): Settings['FileCount'][1] = True if o in ("-r", "--raw"): ShowRaw = True if o in ("-a", "--avgfilesize"): Settings['AvgFileSize'][1] = True if o in ("-o", "--totalfilesize"): Settings['TotalFileSize'][1] = True if o in ("-a", "--all"): Settings['Timestamp'][1] = True Settings['DirCount'][1] = True Settings['FileCount'][1] = True Settings['AvgFileSize'][1] = True Settings['TotalFileSize'][1] = True if o in ("-h", "--help"): Usage() sys.exit() def Traverse(Path): # Working values. FileCount = 0 DirCount = -1 # Do not include root folder. TotalFileSize = 0 # Some fairly important code! for (Root, Dirs, Files) in os.walk(Path): DirCount += 1 for File in Files: FileCount += 1 TotalFileSize += os.path.getsize(Root + '/' + File) return (FileCount, DirCount, TotalFileSize) def Usage(): print 'Some day this app will be cool enough to have a usage description. Just look at the source code and figure it out.' # Gather directory tree information. (Settings['FileCount'][2], Settings['DirCount'][2], Settings['TotalFileSize'][2]) = Traverse(Settings['RootDir'][2]) Settings['AvgFileSize'][2] = Settings['TotalFileSize'][2] / Settings['FileCount'][2] # Print output. if ShowRaw: for (key, value) in Settings.items(): if value[1]: sys.__stdout__.write(str(value[2]) + ' ') else: for (key, value) in Settings.items(): if value[1]: sys.__stdout__.write(str(value[0]) + ' ' + str(value[2]) + "\n")