-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (51 loc) · 2.21 KB
/
main.py
File metadata and controls
65 lines (51 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import os
import sys
import shutil
from pathlib import Path
# Define categories and their file extensions
FILE_CATEGORIES = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".svg", ".webp"],
"Documents": [".pdf", ".doc", ".docx", ".txt", ".odt", ".xls", ".xlsx", ".ppt", ".pptx", ".csv"],
"Audio": [".mp3", ".wav", ".aac", ".flac", ".ogg", ".m4a"],
"Video": [".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv"],
"Archives": [".zip", ".tar", ".gz", ".rar", ".7z"],
"Scripts": [".py", ".sh", ".js", ".php", ".rb", ".pl"],
"Code": [".html", ".css", ".json", ".xml", ".yml", ".yaml", ".ini", ".cfg"],
"Other": [] # fallback folder
}
def organize_files(folder: Path):
"""Organize files in subfolders based on file type."""
print(f"📁 Starting organization of: {folder}")
for item in folder.iterdir():
if item.is_file():
moved = False
# Determine the correct category for the file
for category, extensions in FILE_CATEGORIES.items():
if item.suffix.lower() in extensions:
target_dir = folder / category
target_dir.mkdir(exist_ok=True)
shutil.move(str(item), str(target_dir / item.name))
print(f" ➜ {item.name} → {category}/")
moved = True
break
# If no category matches, move to "Other"
if not moved:
other_dir = folder / "Other"
other_dir.mkdir(exist_ok=True)
shutil.move(str(item), str(other_dir / item.name))
print(f" ➜ {item.name} → Other/")
print("✅ Done organizing!")
def main():
"""Main function: read the argument and organize the specified folder."""
if len(sys.argv) != 2:
print("Usage: python3 main.py <path_to_folder>")
sys.exit(1)
folder = Path(sys.argv[1]).expanduser().resolve()
if not folder.exists() or not folder.is_dir():
print(f"❌ The specified path does not exist or is not a directory: {folder}")
sys.exit(1)
print(f"📂 Organizing folder: {folder}")
organize_files(folder)
if __name__ == "__main__":
main()