MCPcopy Index your code
hub / github.com/geekcomputers/Python / organize_files

Function organize_files

smart_file_organizer.py:64–93  ·  view source on GitHub ↗

Organize files in the given directory into subfolders by category. Args: base_path: Path of the directory to organize.

(base_path: str)

Source from the content-addressed store, hash-verified

62
63
64def 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
96def main() -> None:

Callers 1

mainFunction · 0.85

Calls 3

get_categoryFunction · 0.85
create_folderFunction · 0.85
moveMethod · 0.45

Tested by

no test coverage detected