Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import os
  2. import glob
  3. import shutil
  4. from easygui import *
  5. import re
  6.  
  7. pattern = re.compile(r'(.\w{1,4})$')
  8.  
  9. file_endings = {'audio':    ['.ogg','.mp3','.wav','.midi'],
  10.                 'pictures': ['.jpg', '.png', '.gif'],
  11.                 'video':    ['.avi', '.mpg', '.mov'],
  12.                 'text':     ['.txt', '.doc', '.docx'],
  13.                 'compr':    ['.rar', '.zip', 'tgz'],
  14.                 'misc':     ['.lnk']}
  15. file_list = dict()
  16.  
  17. for file_end in file_endings.keys(): file_list[file_end] = []
  18.  
  19. print('Select a folder to organize:')
  20. open_directory = diropenbox(msg=None,title=None, default='/~')
  21.  
  22. print('Where do you want the files to be saved?')
  23. save_to = diropenbox(msg=None, title=None, default='/~')
  24.  
  25. os.chdir(open_directory)
  26.  
  27. for file in glob.glob('*.*'):
  28.     if re.search(pattern, file):
  29.         match = re.search(pattern, file)
  30.  
  31.         for key in file_endings:
  32.             if match.group(0) in file_endings[key]:
  33.                 file_list[key].append(file)
  34.  
  35.  
  36. # lets make the directories & copy the files
  37. for key in file_list.keys():
  38.     if len(file_list[key]) > 0:
  39.  
  40.         try:
  41.             os.mkdir(save_to + '/' + key)
  42.             folder = save_to + '/' + key
  43.             print(key, 'folder created')
  44.  
  45.             for file in file_list[key]:
  46.                     shutil.copy(open_directory+'/'+file, folder)
  47.         except:
  48.             print('Couldnt make the directory and therefore not copy the files, maybe you need sudo-permissions')
  49.  
  50. #it went fine, all we need to do is print a nice ending message :-)
  51.  
  52. for key in file_list.keys():
  53.     if len(file_list[key]) > 0:
  54.         print('{0} {1} files copied.'.format(len(file_list[key]), key))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement