Organize files in the given directory into subfolders by category. Args: base_path: Path of the directory to organize.
(base_path: str)
| 62 | |
| 63 | |
| 64 | def organize_files(base_path: str) -> None: |
| 65 | """ |
| 66 | Organize files in the given directory into subfolders by category. |
| 67 | |
| 68 | Args: |
| 69 | base_path: Path of the directory to organize. |
| 70 | """ |
| 71 | files = [ |
| 72 | f for f in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, f)) |
| 73 | ] |
| 74 | if not files: |
| 75 | print(f"[{datetime.now().strftime('%H:%M:%S')}] No files found in {base_path}") |
| 76 | return |
| 77 | |
| 78 | for file_name in files: |
| 79 | source = os.path.join(base_path, file_name) |
| 80 | file_ext = os.path.splitext(file_name)[1] |
| 81 | category = get_category(file_ext) |
| 82 | target_folder = os.path.join(base_path, category) |
| 83 | create_folder(target_folder) |
| 84 | |
| 85 | try: |
| 86 | shutil.move(source, os.path.join(target_folder, file_name)) |
| 87 | print( |
| 88 | f"[{datetime.now().strftime('%H:%M:%S')}] Moved: {file_name} -> {category}/" |
| 89 | ) |
| 90 | except Exception as e: |
| 91 | print( |
| 92 | f"[{datetime.now().strftime('%H:%M:%S')}] Error moving {file_name}: {e}" |
| 93 | ) |
| 94 | |
| 95 | |
| 96 | def main() -> None: |
no test coverage detected