-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestructreLibrary.py
More file actions
83 lines (71 loc) · 2.79 KB
/
restructreLibrary.py
File metadata and controls
83 lines (71 loc) · 2.79 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import re
import shutil
# --- Paths ---
entry_point = "/run/media/mohammed28/Ventoy/New folder/" # messy books
organized_path = "/run/media/mohammed28/Ventoy/Organized_Library" # organized library
os.makedirs(organized_path, exist_ok=True)
# --- File extensions to consider ---
book_extensions = [".epub", ".pdf", ".mobi"]
# --- Category keywords ---
categories_keywords = {
'Linux': ['linux'],
'Rust': ['rust'],
'Haskell': ['haskell'],
'Operating System': ['operating system', 'os'],
'DevOps': ['devops'],
'C': ['c programming', 'c language', 'learn c'],
'C++': ['c++', 'cpp'],
'C#': ['c#'],
'Git': ['git'],
'Linear Algebra': ['linear algebra'],
'Discrete': ['discrete mathematics', 'discrete'],
'Network': ['network'],
'Arabic': [] # handled separately
}
# Dictionary to store categorized files
dic_paths_name_category = {key: [] for key in categories_keywords.keys()}
dic_paths_name_category["Others"] = []
# --- Helper functions ---
def clean_name(name):
# Remove forbidden characters in folder names
return re.sub(r'[\\/:"*?<>|]', "_", name).strip()
def contains_arabic(text):
# Check if string contains Arabic letters
return any('\u0600' <= c <= '\u06FF' for c in text)
# --- Scan files recursively ---
for root, dirs, files in os.walk(entry_point):
for filename in files:
ext = os.path.splitext(filename)[1].lower()
if ext not in book_extensions:
continue
file_name = os.path.splitext(filename)[0]
found_category = False
# Check Arabic first
if contains_arabic(file_name):
dic_paths_name_category['Arabic'].append((filename, root))
found_category = True
else:
# Check other categories using keywords
for category, keywords in categories_keywords.items():
if category == 'Arabic':
continue
for kw in keywords:
if kw.lower() in file_name.lower():
dic_paths_name_category[category].append((filename, root))
found_category = True
break
if found_category:
break
# Uncategorized files
if not found_category:
dic_paths_name_category['Others'].append((filename, root))
# --- Create folders and copy files ---
for category, files in dic_paths_name_category.items():
category_folder = os.path.join(organized_path, clean_name(category))
os.makedirs(category_folder, exist_ok=True)
for f, src_root in files:
src_file = os.path.join(src_root, f)
dst_file = os.path.join(category_folder, f)
shutil.copy2(src_file, dst_file) # preserves metadata
print(f"Copied '{f}' → '{category_folder}/'")