-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (49 loc) · 1.54 KB
/
main.py
File metadata and controls
66 lines (49 loc) · 1.54 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 argparse
from pathlib import Path
from shutil import copyfile
from threading import Thread
import logging
"""
--source [-s]
--output [-o] default folder = dist
"""
""" py . \main.py -s "your_folder_name" """
parser = argparse.ArgumentParser(description="Sorting folder")
parser.add_argument("--source", "-s", help="Source folder", required=True)
parser.add_argument("--output", "-o", help="Output folder", default="dist")
print(parser.parse_args())
args = vars(parser.parse_args())
print(args)
source = Path(args.get("source"))
output = Path(args.get("output"))
folders = []
def grabs_folder(path: Path) -> None:
for el in path.iterdir():
if el.is_dir():
folders.append(el)
grabs_folder(el)
def copy_file(path: Path) -> None:
for el in path.iterdir():
if el.is_file():
ext = el.suffix[1:]
ext_folder = output / ext
try:
ext_folder.mkdir(exist_ok=True, parents=True)
copyfile(el, ext_folder / el.name)
except OSError as err:
logging.error(err)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(threadName)s %(message)s")
folders.append(source)
grabs_folder(source)
print(folders)
threads = []
for folder in folders:
th = Thread(target=copy_file, args=(folder,))
th.start()
threads.append(th)
[th.join() for th in threads]
print(f"Можна видалять {source}")