-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (42 loc) · 1.66 KB
/
main.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
import os
class Sorter:
def __init__(self):
self.current_path = os.path.dirname(os.path.realpath(__file__))
self.files = os.listdir()
self.create_ignore()
self.ignore = self.get_ignored()
self.ext = self.get_extension(self.files)
self.create_folder(self.current_path, self.ext)
self.move_files(self.files)
def create_ignore(self):
if not os.path.isfile(self.current_path + "/ignore.txt"):
with open(self.current_path + "/ignore.txt", "w") as f:
f.write("")
def get_ignored(self):
ignore = list()
with open(self.current_path + "/ignore.txt", "r") as f:
for i in f.readlines():
ignore.append(i.strip())
return ignore
def get_extension(self, files):
ext = list()
for i in files:
ext_ = i.split('.')[-1]
if os.path.isfile(self.current_path + "/" + i):
if ext_ not in ext:
ext.append(ext_)
return ext
def create_folder(self, path, ext):
for i in ext:
if i not in self.ignore and "."+i.split('.')[-1] not in self.ignore:
if not os.path.exists(path + "/" + i):
os.makedirs(i)
def move_files(self, files):
for i in files:
if i not in self.ignore and "."+i.split('.')[-1] not in self.ignore:
ext_ = i.split('.')[-1]
if os.path.isfile(self.current_path + "/" + i):
os.rename(self.current_path + "/" + i,
self.current_path + "/" + ext_ + "/" + i)
if __name__ == '__main__':
Sorter()