-
Notifications
You must be signed in to change notification settings - Fork 2
/
file-explorer.py
228 lines (210 loc) · 6.78 KB
/
file-explorer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from tkinter import *
from time import sleep as slp
import os
import random as rdm
import shutil as sl
import datetime as dt
from tkinter import simpledialog as sd
from tkinter import messagebox as msg
#For randomly selecting colours
rdmc = rdm.choice(["green","blue","white","yellow","orange","pink","purple"])
#This function is for deleting folders or files
def delete_mn_btn():
pth=ent.get()+"/"+g.get(g.curselection()[0])
try:
if (os.path.isdir(pth) == True):
sl.rmtree(pth)
btn_path_fun()
msg.showinfo("File","Folder is deleted.")
else:
os.remove(pth)
msg.showinfo("File","File is deleted.")
btn_path_fun()
except:
msg.showerror("File","Error while deleting.")
#This function is for executing files
def show_opt():
file = ent.get()+"/"+g.get(g.curselection()[0])
os.startfile(file)
#This function is for clearing File listbox
def cllt():
g.delete("0",END)
#This function is for copying the path of file or folder
def btn_copy_fun():
t.clipboard_clear()
t.clipboard_append(ent.get())
#This is function is for creating file
def c_n_flr():
try:
f = sd.askstring("File","Enter filename.")
os.mkdir(ent.get()+"/"+f)
msg.showinfo("File","New directory is created.")
cllt()
btn_path_fun()
except:
msg.showerror("Error","Not able to make new directory.")
#This function is for clearing list box
def clet():
ent.delete(0,END)
#This function is for displaying list of directories of desktop in listbox
def des_btn():
clet()
cllt()
path = "C:/Users/"+os.getlogin()+"/Desktop"
try:
s= os.listdir(path)
n=0
ent.insert("1",path)
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#This function is for displaying list of directories of music in listbox
def mus_btn():
cllt()
clet()
path = "C:/Users/"+os.getlogin()+"/Music"
try:
s= os.listdir(path)
n=0
ent.insert("1",path)
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#This function is for displaying list of directories of picture in listbox
def pic_btn():
cllt()
clet()
path = "C:/Users/"+os.getlogin()+"/Pictures"
try:
s= os.listdir(path)
n=0
ent.insert("1",path)
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#This function is for displaying list of directories of video in listbox
def vid_btn():
cllt()
clet()
path = "C:/Users/"+os.getlogin()+"/Videos"
try:
s= os.listdir(path)
n=0
ent.insert("1",path)
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#This function is for displaying list of directories of download in listbox
def down_btn():
cllt()
clet()
path = "C:/Users/"+os.getlogin()+"/Downloads"
try:
s= os.listdir(path)
n=0
ent.insert("1",path)
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#This function is for displaying list of directories of downloads in listbox
def doc_btn():
cllt()
clet()
path = "C:/Users/"+os.getlogin()+"/Documents"
try:
s= os.listdir(path)
n=0
ent.insert("1",path)
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#This function is for copying file
def copy_fl():
try:
a=ent.get()+"/"+g.get(g.curselection()[0])
b=ent.get()+"/"+str(dt.datetime.now()).replace(".","").replace(":","")+" - "+g.get(g.curselection()[0])
if(os.path.isdir(a) == "True"):
sl.copytree(a,b)
else:
sl.copy(a,b)
btn_path_fun()
except Exception as err:
print(err)
msg.showerror("Error","Error while copying")
#This function is for deleting selected file
def opn_fl():
path=ent.get()+"/"+g.get(g.curselection()[0])
if (os.path.isdir(path) == True):
clet()
ent.insert("1",path)
cllt()
btn_path_fun()
else:
show_opt()
btn_path_fun()
#This function is for filling the file listBox
def btn_path_fun():
cllt()
path = ent.get()
try:
s= os.listdir(path)
n=0
while(len(s)>n):
g.insert(n,s[n])
n=n+1
except:
msg.showerror("Error","Path not found")
#GUI of application
t = Tk()
t.title("Abhineet Raj - FILE Explorer")
t.geometry("600x400")
bg= Label(t, height="400", width="500", background=rdmc)
lb = Label(t, text="Path", font=40, background=rdmc)
ent=Entry(font=40, width="35")
ent.insert("1",("C:/Users/"+os.getlogin()+"/Desktop"))
btn_ist=Button(t,command=btn_path_fun ,text="List",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_copy=Button(t,command=btn_copy_fun ,text="Copy",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_de = Button(t,command=des_btn ,text="Desktop",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_doc = Button(t,command=doc_btn ,text="Documents",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_pic = Button(t,command=mus_btn, text="Pictures",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_mus = Button(t,command=pic_btn, text="Music",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_vid = Button(t,command=vid_btn, text="Video",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn_down = Button(t,command=down_btn, text="Downloads",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn = Button(t,command=c_n_flr ,text="Create new Folder",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn1 = Button(t,command=delete_mn_btn, text="Delete",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn2 = Button(t,command=opn_fl, text="Open",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
btn5 = Button(t,command=copy_fl, text="Copy file",font=40 ,activebackground="green", activeforeground="white", background="white", relief="groove")
m = Scrollbar(t, orient="vertical")
g = Listbox(t, height=14, width=60, yscrollcommand=m.set)
#Positioning of widgets
m.configure(command=g.yview)
bg.place(x=0,y=0)
lb.place(x=0,y=350)
btn_ist.place(x=530,y=350)
btn_copy.place(x=450,y=350)
ent.place(x=50,y=350)
btn_de.place(x=0,y=0)
btn_doc.place(x=0,y=40)
btn_pic.place(x=0,y=80)
btn_mus.place(x=0,y=120)
btn_vid.place(x=0,y=160)
btn_down.place(x=0,y=200)
btn.place(x=170,y=230)
btn1.place(x=250,y=270)
btn2.place(x=170,y=270)
btn5.place(x=330,y=270)
g.place(x=135,y=0)
m.place(x=510,y=0)
t.mainloop()