-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileOrganizer.py
177 lines (167 loc) · 7.42 KB
/
fileOrganizer.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Libraries used
import os
import sys
import shutil
import ntpath
# Dictionary to specify different types of extensions
DIRECTORIES = {
"HTML": [".html5", ".html", ".htm", ".xhtml"],
"IMAGES": [".jpeg", ".jpg", ".tiff", ".gif", ".bmp", ".png", ".bpg", "svg",
".heif", ".psd", ".jfif"
],
"FILE": ["File"],
"VIDEOS": [".avi", ".flv", ".wmv", ".mov", ".mp4", ".webm", ".vob", ".mng",
".qt", ".mpg", ".mpeg", ".3gp"
],
"DOCUMENTS": [".oxps", ".epub", ".pages", ".docx", ".doc", ".fdf", ".ods",
".odt", ".pwi", ".xsn", ".xps", ".dotx", ".docm", ".dox",
".rvg", ".rtf", ".rtfd", ".wpd", ".xls", ".xlsx", ".ppt",
"pptx"
],
"ARCHIVES": [".a", ".ar", ".cpio", ".iso", ".tar", ".gz", ".rz", ".7z",
".dmg", ".rar", ".xar", ".zip"
],
"AUDIO": [".aac", ".aa", ".aac", ".dvf", ".m4a", ".m4b", ".m4p", ".mp3",
".msv", "ogg", "oga", ".raw", ".vox", ".wav", ".wma"
],
"PLAINTEXT": [".txt", ".in", ".out"],
"PDF": [".pdf"],
"PYTHON": [".py"],
"XML": [".xml"],
"EXE": [".exe"],
"SHELL": [".sh"],
"JS": ["javaScript File"],
"torrent": ["TORRENT File"],
"JSON": ["JSON File"]
}
FILE_FORMATS = {file_format: directory
for directory, file_formats in DIRECTORIES.items()
for file_format in file_formats}
# Function which organizes the files according to the extension.
def organizeByExtension():
# returns the Current Working Directory(CWD) of the file.
inputFilePath = os.getcwd()
for eachFile in os.scandir():
if not eachFile.is_dir():
# storing the path in a variable
filePath = os.path.abspath(eachFile)
# storing the extension of file inside a variable
fileExtension = os.path.splitext(filePath)[1].lower()
# checking the extension with the File_Formats
if fileExtension in FILE_FORMATS:
destParentFolder = os.path.join(inputFilePath, "Organized")
# checking if the directory is present if not then
# creating the directory named as Organized
if not os.path.exists(destParentFolder):
os.mkdir(destParentFolder)
# creating the new folders as naming
# inside the dictionary above
destFolder = os.path.join(
destParentFolder, FILE_FORMATS[fileExtension])
# checking if already present
if not os.path.exists(destFolder):
os.mkdir(destFolder)
# moving the file to the created directory
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
# removing the older path of file
if os.path.exists(Destination):
os.remove(filePath)
# Function which organizes the files according to the alphabetical order.
def organizeByAlphabet():
# gets the present directory path
inputFilePath = os.getcwd()
for eachFile in os.scandir():
if not eachFile.is_dir():
# gets the path of each file inside the directory
filePath = os.path.abspath(eachFile)
# creating a new directory named Organized
destParentFolder = os.path.join(inputFilePath, "Organized")
# checking if the directory already exists
# if not then create the directory named as Organized
if not os.path.exists(destParentFolder):
os.mkdir(destParentFolder)
# code for checking each file by the alphabets
# pylint: disable=unused-argument
_, tail = ntpath.split(filePath)
# head = head+1
alp = tail[0].upper()
destFolder = os.path.join(destParentFolder, alp)
# making new directory according to the alphabet
if not os.path.exists(destFolder):
os.mkdir(destFolder)
# moving the files to the new directory created
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
# removing the old file path
if os.path.exists(Destination):
os.remove(filePath)
# Function which organizes the files according to the Size of the files
def organizeBySize():
inputFilePath = os.getcwd()
for eachFile in os.scandir():
if not eachFile.is_dir():
filePath = os.path.abspath(eachFile)
# the size of the file in bytes.
size = os.stat(eachFile).st_size
destParentFolder = os.path.join(inputFilePath, "Organized")
if not os.path.exists(destParentFolder):
os.mkdir(destParentFolder)
# For the files sized in Bytes
if 0 <= size < 1000:
destFolder = os.path.join(destParentFolder, "BYTES")
if not os.path.exists(destFolder):
os.mkdir(destFolder)
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
if os.path.exists(Destination):
os.remove(filePath)
# For the files sized in KB's only
elif 1000 < size < 1000000:
destFolder = os.path.join(destParentFolder, "KB")
if not os.path.exists(destFolder):
os.mkdir(destFolder)
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
if os.path.exists(Destination):
os.remove(filePath)
# For the files size uptil 100 MB
elif 1000000 < size < 100000000:
destFolder = os.path.join(destParentFolder, "100MB")
if not os.path.exists(destFolder):
os.mkdir(destFolder)
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
if os.path.exists(Destination):
os.remove(filePath)
# For files size uptil 500 MB
elif 100000000 < size < 500000000:
destFolder = os.path.join(destParentFolder, "500MB")
if not os.path.exists(destFolder):
os.mkdir(destFolder)
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
if os.path.exists(Destination):
os.remove(filePath)
# For files more than a GB
elif size > 1000000000:
destFolder = os.path.join(destParentFolder, "GB")
if not os.path.exists(destFolder):
os.mkdir(destFolder)
shutil.copy2(filePath, destFolder)
Destination = os.path.join(destFolder, eachFile)
if os.path.exists(Destination):
os.remove(filePath)
# Main function/Source
if __name__ == '__main__':
# Taking the input from Command line using Command line parsing.
if len(sys.argv) == 1:
organizeByAlphabet()
elif len(sys.argv) == 2:
Organize = sys.argv[1]
if Organize == "ext":
organizeByExtension()
elif Organize == "size":
organizeBySize()
elif Organize == "alpha":
organizeByAlphabet()