-
- import os
-
- VersionNum = 'ALPHA'
-
-
- def GetDirectorySize(directory):
- """
- Returns interger / float
- Returns the size (in btyes) of a given directory or file.
- """
-
- tmpTotalBytez = 0 #Variable to store culumative size of directory.
-
- #check if directory is in fact a directory (and not a file).
- if os.path.isdir(directory):
-
- #walk through directory and each sub directory and add size of each file.
- for root, dirs, files in os.walk(directory):
- for file in files:
- try:
- tmpTotalBytez += os.path.getsize(root + '\\' + file)
- except:
- pass #Probably because of access denied: TODO: implement a way to track these.
-
- #check is directory is a file.
- elif os.path.isfile(directory):
- tmpTotalBytez = os.path.getsize(directory)
-
- return tmpTotalBytez
-
- def ClearScreen():
- """
- Clears the whole console screen
- """
-
- if os.name == "posix":
- # Unix/Linux/MacOS/BSD/etc
- os.system('clear')
- elif os.name in ("nt", "dos", "ce"):
- # DOS/Windows
- os.system('CLS')
- else:
- # Fallback for other operating systems.
- print ('\n' * 100)
-
-
-
- def UpOneLevel(directory):
- """
- Retians a string
- Removes the last folder from a string containing a path (nt only)
- """
-
- tail = ''
- while tail == '': #moves last directory
- directory, tail = os.path.split(directory)
- if directory.endswith(':\\'): break #if this is the root of Windows drive break loop
-
- if directory.endswith('\\') == False: #ensures '\\' is added incase it is not
- directory = directory + '\\'
- return directory
- def GetDirectorySizes(directory, Verbose = False):
- """
- Returns a list.
- list contains a list of the name and size (in bytes) of each object in the directory.
- [[objectName,objectSize]].
- Pass Verbose = True to enable some console prints of progress.
- """
-
- # create a fresh list to store name and size of each folder and file
- FolderSizeList = []
- TotalRootSize = 0
-
- CurrentFile = 0
-
- # grab objects in current directory
- if Verbose == True:
- tmpNumOfDirs = len(os.listdir(directory))
- tmpCurrentDir = 1
-
- for RootDirs in os.listdir(directory):
-
- if Verbose == True:
- ClearScreen()
- print('Currently calculating ' + RootDirs + ' (' + str(tmpCurrentDir) + '/' + str(tmpNumOfDirs) + ')' )
- tmpCurrentDir += 1
-
- tmp = GetDirectorySize(directory + '\\' + RootDirs)
- FolderSizeList.append([RootDirs, tmp])
-
-
-
- return FolderSizeList
-
- def MainMenu(directory):
-
- """
- This will present a main screen
- show directories of /path
- Continues presenting directory until user states a directory to use.
- """
-
- OldDirectory = directory #back up incase moving into a directory fails (due to access denied or something)
- while True:
-
-
-
- #Main Menu
- ClearScreen()
- print ('Welcome to DirectorySizes')
- print ('Version: ' + VersionNum)
- print ()
- print ('Current Directory: ' + directory)
-
- Indices = 0 # used to present options
- OptionList = [] #create a blank list to store options
-
- #stops runtime error on access denied.
- try:
- SubDirList = os.listdir(directory)
- except:
- input('Access Denied (5): Returning to previous directory, Press any key')
- directory = OldDirectory
- SubDirList = os.listdir(directory)
- print ('Welcome to DirectorySizes')
- print ('Version: ' + VersionNum)
- print ()
- print ('Current Directory: ' + directory)
-
-
- for Directory in SubDirList:
- if os.path.isdir(directory + Directory):
- printline = '{0:4}' + (' ' * 2) + '{1:24}'
- print(printline.format(str(Indices) + ')', Directory)) #Prints each folder in the current directory
- Indices += 1
- OptionList.append(Directory)
- #Option for going up one level
- OptionList.append('Up one level')
- printline = '{0:4}' + (' ' * 2) + '{1:24}'
- print(printline.format(str(Indices) + ')','Up one level'))
- Indices += 1
-
- #Option Use Current
- OptionList.append('Use current directory')
- printline = '{0:4}' + (' ' * 2) + '{1:24}'
- print(printline.format(str(Indices) + ')','Use current directory'))
- print
-
- #grab user input
-
- SelectedOption = int(input('Select an option; '))
-
- #return a value
-
- if OptionList[SelectedOption] == 'Up one level':
- OldDirectory = directory
- directory = UpOneLevel(directory)
- #loop code
-
-
- elif OptionList[SelectedOption] == 'Use current directory':
- #Complete = True
- return directory
-
-
- else:
- OldDirectory = directory
- directory = directory + OptionList[SelectedOption] + '\\'
- #loop code
-
- def ConvertByteSize(SizeInBytes, form = 'Standard' ):
- """
- Converts an amount of bytes, given as an interger, into KB, MB etc...
- returns a list [value(int/float), format(string)]
- Will convert based on algorythm or to a format given as form.
- Options for form are:
- Standard,
- B,
- KB,
- MB,
- GB.
- """
- tmpTotalBytez = [0,'']
- tmpTotalBytez[0] = SizeInBytes
-
- #Based on 'form' paramater, select a format to give back the data
- if form == 'GB':
- tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024/1024 # convert to GB
- tmpTotalBytez[1] = 'GB'
- return tmpTotalBytez
-
- elif form == 'MB':
- tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024 # convert to MB
- tmpTotalBytez[1] = 'MB'
- return tmpTotalBytez
-
- elif form == 'KB':
- tmpTotalBytez[0] = tmpTotalBytez[0]/1024 # convert to KB
- tmpTotalBytez[1] = 'KB'
- return tmpTotalBytez
-
- elif form == 'B':
- tmpTotalBytez[1] = 'B'
- return tmpTotalBytez
-
- else:
-
- if tmpTotalBytez[0] > 1024*1024*1024*1.3:
- tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024/1024 # convert to GB
- tmpTotalBytez[1] = 'GB'
- return tmpTotalBytez
-
- elif tmpTotalBytez[0] > 1024*1024*1.3:
- tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024 # convert to MB
- tmpTotalBytez[1] = 'MB'
- return tmpTotalBytez
-
- elif tmpTotalBytez[0] > 1024*1.3:
- tmpTotalBytez[0] = tmpTotalBytez[0]/1024 # convert to KB
- tmpTotalBytez[1] = 'KB'
- return tmpTotalBytez
-
- else:
- tmpTotalBytez[1] = 'B'
- return tmpTotalBytez
-