Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import glob
- import shutil
- from easygui import *
- import re
- pattern = re.compile(r'(.\w{1,4})$')
- file_endings = {'audio': ['.ogg','.mp3','.wav','.midi'],
- 'pictures': ['.jpg', '.png', '.gif'],
- 'video': ['.avi', '.mpg', '.mov'],
- 'text': ['.txt', '.doc', '.docx'],
- 'compr': ['.rar', '.zip', 'tgz'],
- 'misc': ['.lnk']}
- file_list = dict()
- for file_end in file_endings.keys(): file_list[file_end] = []
- print('Select a folder to organize:')
- open_directory = diropenbox(msg=None,title=None, default='/~')
- print('Where do you want the files to be saved?')
- save_to = diropenbox(msg=None, title=None, default='/~')
- os.chdir(open_directory)
- for file in glob.glob('*.*'):
- if re.search(pattern, file):
- match = re.search(pattern, file)
- for key in file_endings:
- if match.group(0) in file_endings[key]:
- file_list[key].append(file)
- # lets make the directories & copy the files
- for key in file_list.keys():
- if len(file_list[key]) > 0:
- try:
- os.mkdir(save_to + '/' + key)
- folder = save_to + '/' + key
- print(key, 'folder created')
- for file in file_list[key]:
- shutil.copy(open_directory+'/'+file, folder)
- except:
- print('Couldnt make the directory and therefore not copy the files, maybe you need sudo-permissions')
- #it went fine, all we need to do is print a nice ending message :-)
- for key in file_list.keys():
- if len(file_list[key]) > 0:
- print('{0} {1} files copied.'.format(len(file_list[key]), key))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement