-
Notifications
You must be signed in to change notification settings - Fork 1
/
organize.py
83 lines (67 loc) · 1.75 KB
/
organize.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
"""
Name: File Organizer [organise.py]
Description: This Python script organizes files in a given path
@Author: @GaganGulyani
Github Repo: https://github.com/gagangulyani/File-Organizer
"""
from pathlib import Path
from sys import argv
from shutil import move
def get_dir(filename):
"""
This function takes filename and returns name of the
parent directory of the respective filename
Returns Miscellaneous if Parent is not found
"""
ext = filename.suffix[1:]
return dirs.get(ext, "Miscellaneous")
dirs = {
# Images
"jpeg": "Images",
"png": "Images",
"jpg": "Images",
"tiff": "Images",
"gif": "Images",
# Videos
"mp4": "Videos",
"mkv": "Videos",
"mov": "Videos",
"webm": "Videos",
"flv": "Videos",
# Music
"mp3": "Music",
"ogg": "Music",
"wav": "Music",
"flac": "Music",
# Program Files
"py": "Program Files",
"js": "Program Files",
"cpp": "Program Files",
"html": "Program Files",
"css": "Program Files",
"c": "Program Files",
"sh": "Program Files",
# Documents
"pdf": "Documents",
"doc": "Documents",
"docx": "Documents",
"txt": "Documents",
"ppt": "Documents",
"ods": "Documents",
"csv": "Documents"
}
if len(argv) != 2:
print("=" * 35)
print("[ERROR] Invalid number of arguments were given")
print(f"[Usage] python {Path(__file__).name} <dir_path>")
print("=" * 35)
exit(1)
# Directory Path
PATH = Path(argv[1])
for filename in PATH.iterdir():
path_to_file = filename.absolute()
if path_to_file.is_file():
destination = PATH / get_dir(filename)
if not destination.exists():
destination.mkdir()
move(str(path_to_file), str(destination))