Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4.  
  5. print(os.getlogin())
  6. from_path = "C:\\users\\" + os.getlogin()
  7. to = sys.argv[1]
  8.  
  9. def start():
  10.   if len(sys.argv) > 0:
  11.     if os.path.isdir(to):
  12.       print("Path to set to: "+to)
  13.       print("Searching for files...")
  14.  
  15.       cluster_files = []
  16.       cluster_indexes = []
  17.       cluster_sorted = []
  18.       size = 0
  19.       for dirname, dirnames, filenames in os.walk(from_path):
  20.         if(not dirname.startswith(".")):
  21.           for filename in filenames:
  22.             path = os.path.join(dirname, filename)
  23.             if(not filename.startswith(".")):
  24.               try:
  25.                 crr_size = get_size(path)
  26.                 size += crr_size
  27.                 add = True
  28.               except:
  29.                 print("File '" + path + "': permission denied")
  30.                 add = False
  31.               if(add):
  32.                 cluster_files.append(path)
  33.                 cluster_indexes.append(int(crr_size))
  34.             else:
  35.               print("File '" + path + "': no need to copy this file")
  36.         else:
  37.           print("Dir '" + dirname + "': no need to copy this dir")
  38.       print("--------------------------------------------------")
  39.       print("Sorting files by size.")
  40.       cluster_sorted = sort_files(list(cluster_indexes), list(cluster_files))
  41.       print("Sorting finished.")
  42.       print("Cluster length: " + str(len(cluster_sorted)))
  43.       print("File size in GB: " + str(float((((size)/1024)/1024)/1024)))
  44.       last_size = 0
  45.       good = 0
  46.       badd = 0
  47.       for a in cluster_sorted:
  48.         if(get_size(a) >= last_size):
  49.           good += 1
  50.         else:
  51.           badd += 1
  52.         last_size = get_size(a)
  53.     else:
  54.       print(to + " is not a directory.")
  55.  
  56.   else:
  57.     print("Missing an argument")
  58.  
  59. def get_size(filename):
  60.   try:  
  61.     st = os.stat(filename)
  62.     return st.st_size
  63.   except:
  64.     print("File has not been found")
  65.  
  66.  
  67. def sort_files(array_indexes, array_files):#from http://stackoverflow.com/questions/18262306/quick-sort-with-python edited by me lol
  68.   less = []
  69.   equal = []
  70.   greater = []
  71.  
  72.   if len(array_indexes) > 1:
  73.     pivot = array_indexes[0]
  74.     index = 0
  75.     for x in list(array_indexes):
  76.       if x < pivot:
  77.         less.append(array_files[index])
  78.       if x == pivot:
  79.         equal.append(array_files[index])
  80.       if x > pivot:
  81.         greater.append(array_files[index])
  82.       index += index
  83.     return sort_files(less, array_files)+equal+sort_files(greater, array_files)
  84.   else:
  85.     return array_files
  86.  
  87. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement