-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_organizer.py
More file actions
130 lines (102 loc) · 5.1 KB
/
file_organizer.py
File metadata and controls
130 lines (102 loc) · 5.1 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
import os
from pathlib import Path
import shutil # Operations on files and collection of files
# get the Username of the current user in the system
username = os.getlogin()
# changes the directory to the working directory
os.chdir(f"/Users/{username}/Downloads")
# audio extension list
audio = (".3ga", ".aac", ".ac3", ".aif", ".aiff", ".alac", ".amr", ".ape",
".au", ".dss", ".flac", ".flv", ".m4a", ".m4b", ".m4p", ".mp3",
".mpga", ".ogg", ".oga", ".mogg", ".opus", ".qcp", ".tta", ".voc",
".wav", ".wma", ".wv")
# video extension list
video = (".webm", ".MTS", ".M2TS", ".TS", ".mov",
".mp4", ".m4p", ".m4v", ".mxf")
# image extension list
img = (".jpg", ".jpeg", ".jfif", ".pjpeg", ".pjp", ".png",
".gif", ".webp", ".svg", ".apng", ".avif")
# ebook extension list
ebook = (".txt", ".epub", ".mobi", ".azw", ".azw3", ".pdf")
# executable extension list
exe = (".exe", ".bat", ".com", ".cmd", ".inf", ".ipa", ".osx",
".pif", ".run", ".wsh", ".msi")
# office extension list
office = (".xls", ".xlsx", ".doc", ".docx", ".ppt", ".pptx", ".ods",
".xlt", ".csv", ".odt", ".db", ".dotx", ".odf", ".odp")
# compression extension list
zipped = (".apz", ".b6z", ".sy_", ".zst", ".rte", ".rar", ".pup", ".cit",
"bz2", ".sfg", ".s00", ".npk", ".deb", ".pkg", ".tbz", ".sit", ".pwa",
".dar", ".lzm", ".ice", ".qda", ".sqx", ".ita", ".cbt", ".xip", ".ecs",
".dl_", ".gza", ".zpi", ".pit", ".cb7", ".p7z", ".ctx", ".hbe", ".par",
".cbr", ".vip", ".uha", ".r00", ".opk", ".zip", ".f3z", ".rev", ".taz",
".kgb", ".epi", ".cbz", ".pak", ".sfx", ".nex", ".a02", ".pcv", ".z03",
".rpm", ".cdz", ".c00", ".s7z", ".hki", ".tx_", ".ari", ".lz4", ".zix",
".whl", ".apk", ".ark", ".tgs", ".ayt", ".sfs", ".a01", ".ipk", ".c10",
".spd", ".gmz", ".ace", ".arc", ".r01", ".piz", ".z00", ".spa", ".pea",
".cba", ".war", ".fdp", ".sdc", ".snb", ".s02", ".r03", ".ctz", ".ubz",
".oar", ".pkz", ".gca", ".p19", ".a00", ".r30", ".mzp", ".c01", ".tgz",
".alz", ".rnc", ".rss", ".s01", ".vpk", ".jex", ".spt", ".pup", ".car",
".arj", ".xar", ".lzh", ".fp8", ".jhh", ".000", ".jgz", ".z04", ".rp9",
".lbr", ".zoo", ".shr", ".pet", ".sar", ".xez", ".tcx", ".lha", ".sea",
".hyp", ".pax", ".dgc", ".spl", ".spm", ".psz", ".shk", ".mzp", ".r04",
".mbz", ".ain", ".zap", ".xef", ".hbc", ".lqr", ".bza", ".pbi", ".ize",
".isx", ".z02", ".xoj", ".lzr", ".edz", ".wdz", ".gzi", ".gz2", ".vib",
".cpt", ".nar", ".txz", ".egg", ".ipg", ".z01", ".wux", ".b64", ".zi_",
".vsi", ".c02", ".hpk", ".sdn", ".ish", ".tlz", ".prs", ".uc2", ".xzm",
".lzo", ".mou", ".arh", ".mar", ".daf", ".stg", ".vwi", ".snz", ".zim",
".puz", ".jic", ".yz1", ".pae", ".r02", ".pim", ".wot", ".gar", ".trs",
".lzx", ".r21", ".p01", ".wlb", ".efw", ".sen", ".vms", ".pxl", ".ana",
".sqf", ".fcx", ".sfm", ".s09", ".cp9", ".boo", ".sbx", ".zed", ".vem",
".vfs", ".sbx", ".sqz", ".xfp")
# checks if an extension is in one of the previous list and returns True, otherwise, returns False
def is_audio(file):
return os.path.splitext(file)[1].lower() in audio
def is_video(file):
return os.path.splitext(file)[1].lower() in video
def is_image(file):
return os.path.splitext(file)[1].lower() in img
def is_screenshot(file):
name, ext = os.path.splitext(file)
return (ext in img) and "screenshot" in name.lower()
def is_ebook(file):
return os.path.splitext(file)[1].lower() in ebook
def is_exe(file):
return os.path.splitext(file)[1].lower() in exe
def is_zipped(file):
return os.path.splitext(file)[1].lower() in zipped
def is_office(file):
return os.path.splitext(file)[1].lower() in office
# creates directories if they don't exist
Path("audio").mkdir(exist_ok=True)
Path("video").mkdir(exist_ok=True)
Path("image").mkdir(exist_ok=True)
Path("screenshot").mkdir(exist_ok=True)
Path("ebook").mkdir(exist_ok=True)
Path("misc").mkdir(exist_ok=True)
Path("exe").mkdir(exist_ok=True)
Path("office").mkdir(exist_ok=True)
Path("compressed").mkdir(exist_ok=True)
# iterates on each file in the directory and if the extension is in one of the lists, move the file to the correct directory
for file in os.listdir():
if is_audio(file):
shutil.move(file, f"/Users/{username}/Downloads/audio")
elif is_video(file):
shutil.move(file, f"/Users/{username}/Downloads/video")
elif is_ebook(file):
shutil.move(file, f"/Users/{username}/Downloads/ebook")
elif is_exe(file):
shutil.move(file, f"/Users/{username}/Downloads/exe")
elif is_zipped(file):
shutil.move(file, f"/Users/{username}/Downloads/compressed")
elif is_office(file):
shutil.move(file, f"/Users/{username}/Downloads/office")
elif is_image(file):
if is_screenshot(file):
shutil.move(file, f"/Users/{username}/Downloads/screenshot")
else:
shutil.move(file, f"/Users/{username}/Downloads/image")
# else:
# shutil.move(file, f"/Users/{username}/Downloads/misc")
# an output message
print(f"Files have been organized!")