-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
148 lines (109 loc) · 3.86 KB
/
main.py
File metadata and controls
148 lines (109 loc) · 3.86 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys
import scan
import shutil
import normalize
from pathlib import Path
IMAGES = Path('./IMAGES')
IMAGES.mkdir(exist_ok=True)
DOCUMENTS = Path('./DOCUMENTS')
DOCUMENTS.mkdir(exist_ok=True)
AUDIO = Path('./AUDIO')
AUDIO.mkdir(exist_ok=True)
VIDEO = Path('./VIDEO')
VIDEO.mkdir(exist_ok=True)
ARCHIVES = Path('./ARCHIVES')
ARCHIVES.mkdir(exist_ok=True)
OTHER = Path('./OTHER')
OTHER.mkdir(exist_ok=True)
def handle_file(path, root_folder, dist):
target_folder = root_folder/dist
target_folder.mkdir(exist_ok=True)
path.replace(target_folder/normalize.normalize(path.name))
def handle_archive(path, root_folder, dist):
target_folder = root_folder / dist
target_folder.mkdir(exist_ok=True)
if str(path.name).endswith('zip'):
new_name = normalize.normalize(path.name.replace(".zip", ''))
elif str(path.name).endswith('gz'):
new_name = normalize.normalize(path.name.replace(".gz", ''))
elif str(path.name).endswith('tar'):
new_name = normalize.normalize(path.name.replace(".tar", ''))
archive_folder = target_folder / new_name
archive_folder.mkdir(exist_ok=True)
try:
shutil.unpack_archive(str(path.resolve()), str(archive_folder.resolve()))
except shutil.ReadError:
archive_folder.rmdir()
return
except FileNotFoundError:
archive_folder.rmdir()
return
path.unlink()
def remove_empty_folders(path):
for item in path.iterdir():
if item.is_dir():
remove_empty_folders(item)
try:
item.rmdir()
except OSError:
pass
def main(folder_path):
#print(folder_path)
scan.scan(folder_path)
for file in scan.jpeg_files:
handle_file(file, folder_path, "IMAGES")
for file in scan.jpg_files:
handle_file(file, folder_path, "IMAGES")
for file in scan.png_files:
handle_file(file, folder_path, "IMAGES")
for file in scan.svg_files:
handle_file(file, folder_path, "IMAGES")
for file in scan.txt_files:
handle_file(file, folder_path, "DOCUMENTS")
for file in scan.doc_files:
handle_file(file, folder_path, "DOCUMENTS")
for file in scan.docx_files:
handle_file(file, folder_path, "DOCUMENTS")
for file in scan.pdf_files:
handle_file(file, folder_path, "DOCUMENTS")
for file in scan.xlsx_files:
handle_file(file, folder_path, "DOCUMENTS")
for file in scan.pptx_files:
handle_file(file, folder_path, "DOCUMENTS")
for file in scan.mp3_files:
handle_file(file, folder_path, 'AUDIO')
for file in scan.ogg_files:
handle_file(file, folder_path, 'AUDIO')
for file in scan.wav_files:
handle_file(file, folder_path, 'AUDIO')
for file in scan.amr_files:
handle_file(file, folder_path, 'AUDIO')
for file in scan.avi_files:
handle_file(file, folder_path, 'VIDEO')
for file in scan.mp4_files:
handle_file(file, folder_path, 'VIDEO')
for file in scan.mov_files:
handle_file(file, folder_path, 'VIDEO')
for file in scan.mkv_files:
handle_file(file, folder_path, 'VIDEO')
for file in scan.zip_files:
handle_archive(file, folder_path, 'ARCHIVES')
for file in scan.gz_files:
handle_archive(file, folder_path, 'ARCHIVES')
for file in scan.tar_files:
handle_archive(file, folder_path, 'ARCHIVES')
for file in scan.others:
handle_file(file, folder_path, "OTHER")
remove_empty_folders(folder_path)
if __name__ == '__main__':
path = sys.argv[1]
print(f'Start in {path}')
folder = Path(path)
main(folder.resolve())
for item in folder.iterdir():
print(item.name, end=' ')
for file in item.iterdir():
print(file.name, end=" ")
print("")
print(f"All extensions: {scan.extensions}")
print(f"Unknown extensions: {scan.unknown}")