spacepaste

  1.  
  2. import os
  3. VersionNum = 'ALPHA'
  4. def GetDirectorySize(directory):
  5. """
  6. Returns interger / float
  7. Returns the size (in btyes) of a given directory or file.
  8. """
  9. tmpTotalBytez = 0 #Variable to store culumative size of directory.
  10. #check if directory is in fact a directory (and not a file).
  11. if os.path.isdir(directory):
  12. #walk through directory and each sub directory and add size of each file.
  13. for root, dirs, files in os.walk(directory):
  14. for file in files:
  15. try:
  16. tmpTotalBytez += os.path.getsize(root + '\\' + file)
  17. except:
  18. pass #Probably because of access denied: TODO: implement a way to track these.
  19. #check is directory is a file.
  20. elif os.path.isfile(directory):
  21. tmpTotalBytez = os.path.getsize(directory)
  22. return tmpTotalBytez
  23. def ClearScreen():
  24. """
  25. Clears the whole console screen
  26. """
  27. if os.name == "posix":
  28. # Unix/Linux/MacOS/BSD/etc
  29. os.system('clear')
  30. elif os.name in ("nt", "dos", "ce"):
  31. # DOS/Windows
  32. os.system('CLS')
  33. else:
  34. # Fallback for other operating systems.
  35. print ('\n' * 100)
  36. def UpOneLevel(directory):
  37. """
  38. Retians a string
  39. Removes the last folder from a string containing a path (nt only)
  40. """
  41. tail = ''
  42. while tail == '': #moves last directory
  43. directory, tail = os.path.split(directory)
  44. if directory.endswith(':\\'): break #if this is the root of Windows drive break loop
  45. if directory.endswith('\\') == False: #ensures '\\' is added incase it is not
  46. directory = directory + '\\'
  47. return directory
  48. def GetDirectorySizes(directory, Verbose = False):
  49. """
  50. Returns a list.
  51. list contains a list of the name and size (in bytes) of each object in the directory.
  52. [[objectName,objectSize]].
  53. Pass Verbose = True to enable some console prints of progress.
  54. """
  55. # create a fresh list to store name and size of each folder and file
  56. FolderSizeList = []
  57. TotalRootSize = 0
  58. CurrentFile = 0
  59. # grab objects in current directory
  60. if Verbose == True:
  61. tmpNumOfDirs = len(os.listdir(directory))
  62. tmpCurrentDir = 1
  63. for RootDirs in os.listdir(directory):
  64. if Verbose == True:
  65. ClearScreen()
  66. print('Currently calculating ' + RootDirs + ' (' + str(tmpCurrentDir) + '/' + str(tmpNumOfDirs) + ')' )
  67. tmpCurrentDir += 1
  68. tmp = GetDirectorySize(directory + '\\' + RootDirs)
  69. FolderSizeList.append([RootDirs, tmp])
  70. return FolderSizeList
  71. def MainMenu(directory):
  72. """
  73. This will present a main screen
  74. show directories of /path
  75. Continues presenting directory until user states a directory to use.
  76. """
  77. OldDirectory = directory #back up incase moving into a directory fails (due to access denied or something)
  78. while True:
  79. #Main Menu
  80. ClearScreen()
  81. print ('Welcome to DirectorySizes')
  82. print ('Version: ' + VersionNum)
  83. print ()
  84. print ('Current Directory: ' + directory)
  85. Indices = 0 # used to present options
  86. OptionList = [] #create a blank list to store options
  87. #stops runtime error on access denied.
  88. try:
  89. SubDirList = os.listdir(directory)
  90. except:
  91. input('Access Denied (5): Returning to previous directory, Press any key')
  92. directory = OldDirectory
  93. SubDirList = os.listdir(directory)
  94. print ('Welcome to DirectorySizes')
  95. print ('Version: ' + VersionNum)
  96. print ()
  97. print ('Current Directory: ' + directory)
  98. for Directory in SubDirList:
  99. if os.path.isdir(directory + Directory):
  100. printline = '{0:4}' + (' ' * 2) + '{1:24}'
  101. print(printline.format(str(Indices) + ')', Directory)) #Prints each folder in the current directory
  102. Indices += 1
  103. OptionList.append(Directory)
  104. #Option for going up one level
  105. OptionList.append('Up one level')
  106. printline = '{0:4}' + (' ' * 2) + '{1:24}'
  107. print(printline.format(str(Indices) + ')','Up one level'))
  108. Indices += 1
  109. #Option Use Current
  110. OptionList.append('Use current directory')
  111. printline = '{0:4}' + (' ' * 2) + '{1:24}'
  112. print(printline.format(str(Indices) + ')','Use current directory'))
  113. print
  114. #grab user input
  115. SelectedOption = int(input('Select an option; '))
  116. #return a value
  117. if OptionList[SelectedOption] == 'Up one level':
  118. OldDirectory = directory
  119. directory = UpOneLevel(directory)
  120. #loop code
  121. elif OptionList[SelectedOption] == 'Use current directory':
  122. #Complete = True
  123. return directory
  124. else:
  125. OldDirectory = directory
  126. directory = directory + OptionList[SelectedOption] + '\\'
  127. #loop code
  128. def ConvertByteSize(SizeInBytes, form = 'Standard' ):
  129. """
  130. Converts an amount of bytes, given as an interger, into KB, MB etc...
  131. returns a list [value(int/float), format(string)]
  132. Will convert based on algorythm or to a format given as form.
  133. Options for form are:
  134. Standard,
  135. B,
  136. KB,
  137. MB,
  138. GB.
  139. """
  140. tmpTotalBytez = [0,'']
  141. tmpTotalBytez[0] = SizeInBytes
  142. #Based on 'form' paramater, select a format to give back the data
  143. if form == 'GB':
  144. tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024/1024 # convert to GB
  145. tmpTotalBytez[1] = 'GB'
  146. return tmpTotalBytez
  147. elif form == 'MB':
  148. tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024 # convert to MB
  149. tmpTotalBytez[1] = 'MB'
  150. return tmpTotalBytez
  151. elif form == 'KB':
  152. tmpTotalBytez[0] = tmpTotalBytez[0]/1024 # convert to KB
  153. tmpTotalBytez[1] = 'KB'
  154. return tmpTotalBytez
  155. elif form == 'B':
  156. tmpTotalBytez[1] = 'B'
  157. return tmpTotalBytez
  158. else:
  159. if tmpTotalBytez[0] > 1024*1024*1024*1.3:
  160. tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024/1024 # convert to GB
  161. tmpTotalBytez[1] = 'GB'
  162. return tmpTotalBytez
  163. elif tmpTotalBytez[0] > 1024*1024*1.3:
  164. tmpTotalBytez[0] = tmpTotalBytez[0]/1024/1024 # convert to MB
  165. tmpTotalBytez[1] = 'MB'
  166. return tmpTotalBytez
  167. elif tmpTotalBytez[0] > 1024*1.3:
  168. tmpTotalBytez[0] = tmpTotalBytez[0]/1024 # convert to KB
  169. tmpTotalBytez[1] = 'KB'
  170. return tmpTotalBytez
  171. else:
  172. tmpTotalBytez[1] = 'B'
  173. return tmpTotalBytez
  174.