-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_organizer.py
122 lines (107 loc) · 3.01 KB
/
file_organizer.py
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
import logging
import os
logging.basicConfig(
level=logging.DEBUG,
format="[%(levelname)s] %(message)s %(asctime)s",
handlers=[logging.FileHandler("file_organizer.log"), logging.StreamHandler()],
)
DIRECTORY_TO_ORGANIZE_PATH = os.getenv("DIRECTORY_TO_ORGANIZE_PATH")
documents_extension = {
"doc": "word",
"docx": "word",
"pdf": "pdf",
"txt": "simple_text",
"ppt": "powerpoint",
"pptx": "powerpoint",
"xls": "excel",
"xlsx": "excel",
"csv": "excel",
}
images_extension = ["jpg", "jpeg", "png", "gif", "svg", "bmp", "tiff", "ico"]
videos_extension = [
"mp4",
"avi",
"mov",
"wmv",
"flv",
"3gp",
"mkv",
"webm",
"mpg",
"mpeg",
"m4v",
]
audios_extension = [
"mp3",
"wav",
"wma",
"ogg",
"aac",
"flac",
"alac",
"aiff",
"pcm",
"aax",
"m4a",
]
compressed_extension = [
"zip",
"rar",
"7z",
"tar",
"gz",
"bz2",
"xz",
"z",
"iso",
"dmg",
"pkg",
"deb",
"rpm",
"sit",
"sitx",
]
dpkg_extension = {
"deb": "dpkg",
}
scripts_extension = {
"py": "python",
"js": "javascript",
"bash": "bash",
"sh": "bash",
}
def get_extension(file: str):
return file.split(".")[-1]
def move_file(file: str, folder: str):
if os.path.exists(f"{DIRECTORY_TO_ORGANIZE_PATH}/{file}"):
if not os.path.exists(folder):
logging.info(f"Creating {folder}")
os.makedirs(folder)
logging.info(f'Moving "{file}" to {folder}')
os.rename(f"{DIRECTORY_TO_ORGANIZE_PATH}/{file}", folder + "/" + file)
if DIRECTORY_TO_ORGANIZE_PATH:
with os.scandir(DIRECTORY_TO_ORGANIZE_PATH) as directory:
for file in directory:
if file.is_file():
file_name = file.name
file_extension = get_extension(file_name)
if file_extension in documents_extension:
move_file(
file_name,
f"{DIRECTORY_TO_ORGANIZE_PATH}/documents/{documents_extension.get(file_extension)}",
)
elif file_extension in images_extension:
move_file(file_name, f"{DIRECTORY_TO_ORGANIZE_PATH}/images")
elif file_extension in videos_extension:
move_file(file_name, f"{DIRECTORY_TO_ORGANIZE_PATH}/videos")
elif file_extension in audios_extension:
move_file(file_name, f"{DIRECTORY_TO_ORGANIZE_PATH}/audios")
elif file_extension in compressed_extension:
move_file(file_name, f"{DIRECTORY_TO_ORGANIZE_PATH}/compressed")
elif file_extension in dpkg_extension:
move_file(file_name, f"{DIRECTORY_TO_ORGANIZE_PATH}/dpkg")
elif file_extension in scripts_extension:
move_file(
file_name,
f"{DIRECTORY_TO_ORGANIZE_PATH}/scripts/{scripts_extension.get(file_extension)}",
)