-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder_dfs.py
146 lines (123 loc) · 3.59 KB
/
folder_dfs.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
import os, glob, shutil, mimetypes, math
from sorting_types.global_variables import SORT_KEYS
from sorting_types.jpg_config import sortJpgByDate
def dictUnion(d1, d2):
'''
Returns dict that is union of two dicts d1, d2
If k is key of d1 but not d2 (or vice versa) then d[k] = d1[k]
If k is key of both d1 and d2 then d[k] = d1[k] + d2[k]
'''
d = {}
for k in d1.keys():
d[k] = d1[k]
for k in d2.keys():
if k in d:
d[k] += d2[k]
else:
d[k] = d2[k]
return d
def fileTypes(fpath):
'''
Returns dict where keys are files' types and values are counts
'''
mime = mimetypes.MimeTypes()
os.chdir(fpath)
types = {}
for item in glob.iglob("*"):
if os.path.isdir(item):
types = dictUnion(types, fileTypes(item))
os.chdir("..")
else:
ext = "NoType"
if "." in item:
ext = item.split(".")[-1]
if ext in types:
types[ext] += 1
else:
types[ext] = 1
return types
def unpack(fpath, destination):
'''
Copies all files from fpath to destination
'''
os.chdir(fpath)
for item in glob.iglob("*"):
if os.path.isdir(item):
unpack(item, destination)
os.chdir("..")
else:
shutil.copy(item, destination)
def sortByType(fpath):
'''
Moves every file into folder with name equals to this type.
For files with no type after "." there is folder called NoType
'''
os.chdir(fpath)
for item in glob.iglob("*"):
if os.path.isdir(item):
continue
else:
ext = "NoType"
if "." in item:
ext = item.split(".")[-1]
if not os.path.isdir(ext):
os.mkdir(ext)
shutil.move(item, ext)
def clearDir(fpath):
'''
Removes everything in fpath
'''
os.chdir(fpath)
for item in glob.iglob("*"):
if os.path.isdir(item):
shutil.rmtree(item)
else:
os.remove(item)
def sortAndRename(fileType):
'''
os.path must be in folder that includes only files with type fileType
This function creates dir "tmp" and moves every file in it.
Then it sorts files, using key from SORT_KEYS.
Moves files out of "tmp" and rename it according to it's position in array.
After all it removes "tmp"
'''
os.mkdir("tmp")
for item in glob.iglob("*"):
shutil.move(item, "tmp")
items = glob.glob("tmp/*")
print("items", items)
items.sort(key = SORT_KEYS[fileType])
name_len = len(str(len(items)))
print("glob *: ", glob.glob("*"))
for i in range(len(items)):
print("file", items[i])
shutil.move(items[i], (("0" * (name_len - len(str(i + 1)))) + str(i + 1)) + "." + fileType)
os.rmdir("tmp")
def sortFiles(fpath):
'''
First moves into fpath.
Then gets all types and sorts it if SORT_KEYS defined
'''
os.chdir(fpath)
types = []
for item in glob.iglob("*"):
if os.path.isdir(item):
types.append(item)
for tp in types:
if tp == "jpg":
os.chdir(tp)
sortJpgByDate()
os.chdir("..")
elif tp in SORT_KEYS:
os.chdir(tp)
sortAndRename(tp)
os.chdir("..")
if __name__ == "__main__":
print("folder_dfs.py called as main")
fpath = input("Enter fpath: ")
temp = input("Enter temp: ")
clearDir(temp)
print(fileTypes(fpath))
unpack(fpath, temp)
sortByType(temp)
sortFiles(temp)