-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileExplorer.py
More file actions
118 lines (94 loc) · 4 KB
/
Copy pathfileExplorer.py
File metadata and controls
118 lines (94 loc) · 4 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
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
import tkinter as tk
import os, pathlib, string
# Window initialize
window = tk.Tk()
# window.resizable(False, False)
window.title("File explorer")
window.geometry('700x500')
# Config
currentFolder = os.getcwd()
lastFolder = os.getcwd() # will change when changecwd is called
# ---------------------------------------------------------------------------- #
# Functions #
# ---------------------------------------------------------------------------- #
def get_drives():
drives = []
for drive in string.ascii_uppercase:
if os.path.exists(f"{drive}:"):
drives.append(drive + ":\\")
return drives
def getFiles(folder):
fileList = os.listdir(currentFolder)
fileList.sort(key=lambda f: os.path.isfile(f))
return fileList
def changecwd(folder):
global currentFolder
global fileList
global fileListBox
lastFolder = os.getcwd()
os.chdir(folder)
currentFolder = os.getcwd()
header.config(text=currentFolder)
fileList = getFiles(currentFolder)
fileListBox.delete(0, tk.END)
fileListBox.insert(tk.END, f"📂 ..")
for file in fileList:
if os.path.isdir(file):
fileListBox.insert(tk.END, f"📂 {file}")
elif os.path.isfile(file):
fileListBox.insert(tk.END, f"📄 {file}")
def clickedFile():
global currentFolder
global fileListBox
global fileList
folder = fileListBox.curselection()
folder = fileListBox.get(folder)[2:len(fileListBox.get(folder))]
if os.path.isdir(folder):
changecwd(folder)
else:
print(f"Opening {folder}")
def clickedFolderofInterest():
folder = foldersOfInterestBox.curselection()
folder = foldersOfInterestBox.get(folder)[2:len(foldersOfInterestBox.get(folder))]
if os.path.isdir(folder):
changecwd(folder)
# # ---------------------------------------------------------------------------- #
# # Frames/Containers #
# # ---------------------------------------------------------------------------- #
topFrame = tk.Frame(window)
topFrame.pack(side=tk.TOP)
leftFrame = tk.Frame(window)
leftFrame.pack(expand = True, fill = tk.BOTH, side=tk.LEFT)
rightFrame = tk.Frame(window)
rightFrame.pack(expand=True, fill=tk.BOTH, side=tk.LEFT)
header = tk.Label(text = currentFolder)
search = tk.Entry(window)
search.insert(0, "Search")
previousfolder = tk.Button(window, text="Back", command=lambda: changecwd(lastFolder))
# # ---------------------------------------------------------------------------- #
# # Widgets #
# # ---------------------------------------------------------------------------- #
# ---------------------------------- Stuff ---------------------------------- #
header.pack(in_=topFrame)
search.pack(in_=topFrame)
previousfolder.pack(in_=topFrame)
# ---------------------------- Folders of interest --------------------------- #
foldersOfInterestBox = tk.Listbox(leftFrame)
foldersOfInterest = get_drives()
for folder in foldersOfInterest:
os.path.normpath(folder)
foldersOfInterestBox.insert(tk.END, f"📂 {folder}")
foldersOfInterestBox.place(relheight=1, relwidth=1, relx=0.5, rely=0.5,anchor=tk.CENTER)
foldersOfInterestBox.bind("<<ListboxSelect>>", lambda event: clickedFolderofInterest())
# --------------------------------- File list -------------------------------- #
fileList = getFiles(currentFolder)
fileListBox = tk.Listbox(rightFrame)
fileListBox.insert(tk.END, f"📂 ..")
for file in fileList:
if os.path.isdir(file):
fileListBox.insert(tk.END, f"📂 {file}")
elif os.path.isfile(file):
fileListBox.insert(tk.END, f"📄 {file}")
fileListBox.place(relheight=1, relwidth=1, relx=0.5, rely=0.5,anchor=tk.CENTER)
fileListBox.bind("<<ListboxSelect>>", lambda event: clickedFile())
window.mainloop()