-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefolder.py
More file actions
40 lines (29 loc) · 1.09 KB
/
makefolder.py
File metadata and controls
40 lines (29 loc) · 1.09 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
import os
import re
import shutil
# -----------------------------
# WORKING DIRECTORY (EDIT THIS)
# -----------------------------
WORKDIR = "/run/media/mohammed28/Ventoy/Organized_Library/Git/"
BOOK_EXT = {".pdf", ".epub", ".mobi"}
def clean(name):
"""Remove illegal characters for folder names."""
return re.sub(r'[\\/:"*?<>|]', "_", name).strip()
# Walk through all subdirectories
for root, dirs, files in os.walk(WORKDIR):
# Prevent creating infinite loops by ignoring already created folders
if root.startswith(os.path.join(WORKDIR, ".")):
continue
for file in files:
ext = os.path.splitext(file)[1].lower()
if ext not in BOOK_EXT:
continue
old_path = os.path.join(root, file)
# Folder name = file name without extension
base_name = clean(os.path.splitext(file)[0])
new_folder = os.path.join(root, base_name)
os.makedirs(new_folder, exist_ok=True)
new_path = os.path.join(new_folder, file)
# Move file
shutil.move(old_path, new_path)
print(f"[MOVED] {file} → {new_folder}/")