-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_File.py
More file actions
80 lines (66 loc) · 1.91 KB
/
Sort_File.py
File metadata and controls
80 lines (66 loc) · 1.91 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
import os
import shutil
from pathlib import Path
import tkinter as tk
from tkinter import filedialog
#tkinter
root = tk.Tk()
root.withdraw()
file_path = filedialog.askdirectory(title="Select a folder!")
print(type(file_path))
#Luu tru
Rfile = open("suffix.txt","r+")
suffix = [i.strip() for i in Rfile.readlines()]
lst_name = os.listdir(file_path)
suffix_folder = []
for i in lst_name:
if (Path(i).suffix == "") and (i[0] == '.'):
suffix_folder.append(i)
print(suffix_folder)
#Ham tim phan mo rong cua file
def findend():
store = []
for i in lst_name:
store.append(Path(i).suffix.lower())
return store
#Ham xoa cac phan tu trung lap trong danh sach cac phan mo rong
def remove_repeat():
store = []
for i in findend():
if (i not in store) and (i!=''):
store.append(i)
return store
#Ham ghi file
def wrt():
with open("suffix.txt","w+") as writefile:
for dir_indx in range(0,len(lst_name)):
if (Path(lst_name[dir_indx]).suffix == "") and (lst_name[dir_indx][0]==".") :
if dir_indx < len(lst_name) - 1:
writefile.writelines(str(lst_name[dir_indx]) + "\n")
else:
writefile.writelines(str(lst_name[dir_indx]))
else:
for i in range (0,len(remove_repeat())):
if i<len(remove_repeat())-1:
writefile.writelines(str(remove_repeat()[i])+"\n")
else:
writefile.writelines(str(remove_repeat()[i]))
wrt()
#Ham tao thu muc moi
def create_new():
for i in remove_repeat():
if (i not in suffix) or ((i not in suffix_folder) and (i != '')):
os.chdir(file_path)
os.mkdir(str(i))
with open("suffix.txt","w+") as w:
w.writelines(str(i))
create_new()
#Ham di chuyen file
def movefile():
for i in lst_name:
if (Path(i).suffix != '') and (i != "suffix.txt"):
path = os.path.join(file_path,i)
snfx = Path(i).suffix
newdirect = os.path.join(file_path,snfx,i)
shutil.move(path,newdirect)
movefile()