├── .gitignore ├── LICENSE ├── README.md ├── organizer.py └── subdirs.json /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Vincent-Gustafsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 📁 Directory Organizer 2 | This python script is all about organizing your files. Practically, this means moving these files into subdirectories based on their filetype (image, video, audio, etc.). The script is easy to understand and extend to your specific use-case. 3 | 4 | ## Installation 5 | ```bash 6 | git clone https://www.github.com/vincent-gustafsson/directory_organizer.git 7 | cd directory_organizer 8 | ``` 9 | 10 | ### Requirements 11 | 📁 Directory Organizer requires Python 3.6+ 12 | 13 | ## Usage 14 | ```bash 15 | python organizer.py path/to/your/downloads/directory path/to/the/subdirectories/file.json 16 | ``` 17 | 18 | ### License 19 | 📁 Directory Organizer is open-source and licensed under the MIT license. -------------------------------------------------------------------------------- /organizer.py: -------------------------------------------------------------------------------- 1 | import json 2 | from argparse import ArgumentParser 3 | from pathlib import Path 4 | from typing import Dict, List 5 | 6 | 7 | def load_subdirs(subdirs_file: Path) -> Dict[str, str]: 8 | return json.load(open(subdirs_file, 'r', encoding='utf-8')) 9 | 10 | 11 | def create_subdirs(dir: Path, subdirs: List[Path]) -> None: 12 | for subdir in subdirs: 13 | (dir / subdir).mkdir(exist_ok=True) 14 | 15 | 16 | def organize_dir(dir: Path, actions: Dict[str, str], other: str) -> None: 17 | # iterate over all files (and not subdirectories) in the directory 18 | for file in dir.glob("*.*"): 19 | if file.is_file(): 20 | try: # if the file has a "known" extension, move it to its proper destination 21 | file_dest = dir / actions[file.suffix.lower()] / file.name 22 | except KeyError: # if the file has an "unkown" extension, move it into the "other" subdirectory 23 | file_dest = dir / other / file.name 24 | 25 | # move the file 26 | file.rename(file_dest) 27 | 28 | 29 | def main(): 30 | parser = ArgumentParser() 31 | parser.add_argument('dir', type=str, metavar='PATH', help='Path to the directory to organize.') 32 | parser.add_argument('subdirs_file', type=str, metavar='PATH', help='Path to the JSON-file containing the subdirectories.') 33 | args = parser.parse_args() 34 | args.dir = Path(args.dir) 35 | args.subdirs_file = Path(args.subdirs_file) 36 | 37 | # check if the directory exists 38 | if not args.dir.is_dir(): 39 | raise ValueError(f'Directory {str(args.dir)} is not an actual directory.') 40 | 41 | # check if the subdirs file is a JSON-file 42 | if not args.subdirs_file.suffix.lower() == '.json': 43 | raise ValueError(f'Subdirectories file {args.subdirs_file} is not an actual JSON-file.') 44 | 45 | # load the dictionary of subdirectories 46 | subdirs = load_subdirs(args.subdirs_file) 47 | 48 | # create a dictionary mapping file extensions to subdirectories 49 | actions = { 50 | ".png": subdirs["images"], 51 | ".jpg": subdirs["images"], 52 | ".gif": subdirs["images"], 53 | 54 | ".mp4": subdirs["videos"], 55 | ".mov": subdirs["videos"], 56 | ".avi": subdirs["videos"], 57 | 58 | ".exe": subdirs["exe_zip"], 59 | ".rar": subdirs["exe_zip"], 60 | ".zip": subdirs["exe_zip"], 61 | 62 | ".wav": subdirs["audio"], 63 | ".mp3": subdirs["audio"], 64 | ".ogg": subdirs["audio"], 65 | ".flac": subdirs["audio"], 66 | } 67 | 68 | # create the subdirectories (if they do not exist yet) 69 | create_subdirs(args.dir, list(subdirs.values())) 70 | 71 | # organize the directory 72 | organize_dir(args.dir, actions, subdirs["other"]) 73 | 74 | 75 | if __name__ == "__main__": 76 | main() 77 | -------------------------------------------------------------------------------- /subdirs.json: -------------------------------------------------------------------------------- 1 | { 2 | "audio": "audio", 3 | "exe_zip": "exe_zip", 4 | "images": "images", 5 | "other": "other", 6 | "videos": "videos" 7 | } --------------------------------------------------------------------------------