|  | 
| 1 | 1 | from shutil import move | 
| 2 |  | -from os import path | 
| 3 | 2 | import os | 
| 4 |  | - | 
| 5 |  | - | 
| 6 |  | -directory = { | 
| 7 |  | -    'Programming Files': set(['ipynb', 'py', 'java', 'cs', 'js', 'vsix', 'jar', 'cc', 'ccc', 'html', 'xml', 'kt']), | 
| 8 |  | -    'Music':  set(['mp3', 'wav', 'wma', 'mpa', 'ram', 'ra', 'aac', 'aif', 'm4a', 'tsa']), | 
| 9 |  | -    'Videos':  set(['mp4', 'webm', 'mkv', 'MPG', 'MP2', 'MPEG', 'MPE', 'MPV', 'OGG', 'M4P', 'M4V', 'WMV', 'MOV', 'QT', 'FLV', 'SWF', 'AVCHD', 'avi', 'mpg', 'mpe', 'mpeg', 'asf', 'wmv', 'mov', 'qt', 'rm']), | 
| 10 |  | -    'Pictures':  set(['jpeg', 'jpg', 'png', 'gif', 'tiff', 'raw', 'webp', 'jfif', 'ico', 'psd', 'svg', 'ai']), | 
| 11 |  | -    'Applications': set(['exe', 'msi', 'deb', 'rpm']), | 
| 12 |  | -    'Compressed': set(['zip', 'rar', 'arj', 'gz', 'sit', 'sitx', 'sea', 'ace', 'bz2', '7z']), | 
| 13 |  | -    'Documents': set(['txt', 'pdf', 'doc', 'xlsx', 'pdf', 'ppt', 'pps', 'docx', 'pptx']), | 
| 14 |  | -    'Other': set([]) | 
| 15 |  | -} | 
| 16 |  | - | 
| 17 |  | - | 
| 18 |  | -def create_folders(): | 
| 19 |  | - | 
|  | 3 | +import json | 
|  | 4 | + | 
|  | 5 | +# Load folder-extension mappings from config.json | 
|  | 6 | +def load_config(file='config.json'): | 
|  | 7 | +    try: | 
|  | 8 | +        with open(file, 'r') as f: | 
|  | 9 | +            return json.load(f) | 
|  | 10 | +    except FileNotFoundError: | 
|  | 11 | +        print(f"Configuration file {file} not found! Using default settings.") | 
|  | 12 | +        return {} | 
|  | 13 | + | 
|  | 14 | +# Create folders based on config.json | 
|  | 15 | +def create_folders(directory): | 
| 20 | 16 |     for dir_ in directory: | 
| 21 | 17 |         try: | 
| 22 | 18 |             os.mkdir(dir_) | 
| 23 | 19 |             print(f'{dir_:20} Created') | 
| 24 | 20 |         except OSError: | 
| 25 | 21 |             print(f'{dir_:20} Already Exists') | 
| 26 | 22 | 
 | 
| 27 |  | - | 
| 28 |  | -def get_folder(ext): | 
| 29 |  | -     | 
| 30 |  | -    for f, ex in directory.items(): | 
| 31 |  | -        if ext in ex: | 
| 32 |  | -            return f | 
|  | 23 | +# Determine which folder a file belongs to | 
|  | 24 | +def get_folder(ext, directory): | 
|  | 25 | +    for folder, extensions in directory.items(): | 
|  | 26 | +        if ext in extensions: | 
|  | 27 | +            return folder | 
| 33 | 28 |     return 'Other' | 
| 34 | 29 | 
 | 
| 35 |  | - | 
| 36 |  | -def start(): | 
|  | 30 | +# Start moving files based on their extensions | 
|  | 31 | +def start(directory): | 
| 37 | 32 |     for filename in os.listdir(): | 
| 38 | 33 |         if filename != __file__ and filename[0] != '.' and '.' in filename: | 
| 39 | 34 |             ext = os.path.basename(filename).split('.')[-1] | 
| 40 |  | -            folder = get_folder(ext) | 
|  | 35 | +            folder = get_folder(ext, directory) | 
| 41 | 36 |             if not os.path.isfile(os.path.join(folder, filename)): | 
| 42 | 37 |                 move(filename, folder) | 
| 43 | 38 | 
 | 
| 44 |  | - | 
| 45 | 39 | if __name__ == '__main__': | 
| 46 |  | -    create_folders() | 
| 47 |  | -    start() | 
|  | 40 | +    config = load_config()  # Load configuration | 
|  | 41 | +    create_folders(config)  # Create necessary folders | 
|  | 42 | +    start(config)           # Start organizing files | 
0 commit comments