Advertisement
Guest User

main

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. from pyds_functions import *
  2.  
  3.  
  4.  
  5. if __name__ == "__main__":
  6.  
  7. user_choice = "l" # Used in main loop
  8. sizes_dict = {} # Stores directory as key and sum of files in that directory (in bytes) as value
  9. while True:
  10.  
  11.     if user_choice == "l":
  12.         print()
  13.         print("Welcome to pyDiskSpace")
  14.         print()
  15.         # Get all drives on system
  16.         drives = get_drives()
  17.  
  18.         # Print drives to screen
  19.         for i, drive in enumerate(drives):
  20.             print(str(i) + ")", drive)
  21.         print()
  22.  
  23.         # Ask user to select a drive
  24.         while True:
  25.             try:
  26.                 drive = int(input("Please select a drive: "))
  27.                 os.chdir(drives[drive])
  28.                 break
  29.  
  30.             except ValueError:
  31.                 print("Invalid input.")
  32.  
  33.             except IndexError:
  34.                 print("Invalid drive.")
  35.  
  36.  
  37.         # Print directories in selected drive to screen
  38.         print_dirs()
  39.  
  40.         user_choice = None
  41.  
  42.     elif user_choice == "u":
  43.         os.chdir("..")
  44.         print_dirs()
  45.  
  46.     elif user_choice == "q":
  47.         break
  48.  
  49.     elif user_choice == "s":
  50.  
  51.         if os.getcwd() in  sizes_dict:
  52.             # Lists each folder inside the directory and it's total size
  53.             return_list = []
  54.             for folder in return_dirs(os.getcwd()):
  55.                 if os.path.isdir(os.getcwd() + "\\" + folder):
  56.                     if len(os.getcwd()) >= 4:
  57.                         foldersize = folder_size(sizes_dict, os.getcwd() + "\\" + folder)
  58.                     else:
  59.                         foldersize = folder_size(sizes_dict, os.getcwd() + folder)
  60.                     return_list.append((folder, foldersize))
  61.  
  62.             print()
  63.             print("Current directory: ", os.getcwd())
  64.             print()
  65.  
  66.             for folder, size in sorted(return_list, key=lambda student: student[1], reverse=True):
  67.                 print(folder, formatbytes(size))
  68.  
  69.             print()
  70.             input("Press any key to continue...")
  71.             print_dirs()
  72.         else:
  73.  
  74.             # Identify how many folders in total
  75.             progress_goal = 0
  76.             for root, dirs, files in os.walk(os.getcwd()):
  77.                 progress_goal += 1
  78.  
  79.             # Gets the size of all directories, including subdirectories
  80.             current_progress = 0
  81.             for root, dirs, files in os.walk(os.getcwd()):
  82.                 current_progress += 1
  83.                 progress = current_progress / progress_goal * 100
  84.                 print("Current progress", "{0:.2f}%".format(progress))
  85.                 if root not in sizes_dict:
  86.  
  87.                     sizes_dict[root] = dir_getsize(root)
  88.  
  89.             # Lists each folder inside the directory and it's total size
  90.             return_list = []
  91.             for folder in return_dirs(os.getcwd()):
  92.                 if os.path.isdir(os.getcwd() + "\\" + folder):
  93.                     if len(os.getcwd()) >= 4:
  94.                         foldersize = folder_size(sizes_dict, os.getcwd() + "\\" + folder)
  95.                     else:
  96.                         foldersize = folder_size(sizes_dict, os.getcwd() + folder)
  97.                     return_list.append((folder, foldersize))
  98.  
  99.             print()
  100.             print("Current directory: ", os.getcwd())
  101.             print()
  102.  
  103.             for folder, size in sorted(return_list, key=lambda student: student[1], reverse=True):
  104.                 print(folder, formatbytes(size))
  105.  
  106.             print()
  107.             input("Press any key to continue...")
  108.             print_dirs()
  109.  
  110.     # Navigate to directory if user selected a one2
  111.     else:
  112.         try:
  113.             user_choice = int(user_choice)
  114.             folders = return_dirs(os.getcwd())
  115.             os.chdir(os.getcwd() + "\\" + folders[user_choice])
  116.  
  117.             # Print directory in new directory to screen
  118.             print_dirs()
  119.  
  120.         except TypeError:
  121.             pass
  122.         except IndexError:
  123.             print("Directory does not exist.")
  124.         except ValueError:
  125.             print("Invalid input.")
  126.  
  127.     user_choice = input("Select number above, (u)p a directory, (s)elect this directory, (l)ist drives or (q)uit: ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement