Repository navigation
Expand file tree
/
Copy path_common.py
More file actions
66 lines (54 loc) · 1.45 KB
/
Copy path_common.py
File metadata and controls
66 lines (54 loc) · 1.45 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
import os
import shutil
from concurrent.futures import ThreadPoolExecutor
from mtlogger import logger
from tqdm import tqdm
from collections.abc import Callable
FILE_BLACKLIST = [
r'cover.jpg',
r'desktop.ini',
r'folder.jpg',
r'.*\.url',
r'.*\.lnk',
]
def find_files_to_process(
parent_folder_path: str,
should_process_item: Callable,
):
files_to_process = []
for item in os.listdir(parent_folder_path):
item_path = os.path.join(parent_folder_path, item)
if should_process_item(item_path):
files_to_process.append(item_path)
return files_to_process
def group_files(
parent_folder_path: str,
files_to_process: list,
get_group_name: Callable,
):
with (
ThreadPoolExecutor() as executor,
tqdm(
total=len(files_to_process), desc=f'Processing "{parent_folder_path}"'
) as progress,
):
for _ in executor.map(
lambda item_path: process_file(
item_path, parent_folder_path, get_group_name
),
files_to_process,
):
progress.update(1)
logger.success(f'Finished grouping files in "{parent_folder_path}".')
def process_file(
item_path: str,
parent_folder_path: str,
get_group_name: Callable,
):
group_name = get_group_name(item_path)
if not group_name:
return
target_folder = os.path.join(parent_folder_path, group_name)
os.makedirs(target_folder, exist_ok=True)
dest = os.path.join(target_folder, os.path.basename(item_path))
shutil.move(item_path, dest)