forked from sofiedroid/fetchfilesize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (153 loc) · 7.92 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
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
import os
import pandas as pd
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
def info():
infowindow = Toplevel()
infolabel = Label(infowindow,
text="This tool 'walks' through a directory and returns the filename, filepath and filesize for all files inside the directory and its subfolders.")
infolabel.grid(row=0, column=0)
blank7 = Label(infowindow, text="")
blank7.grid(row=1, column=0)
infodirectory = Label(infowindow,
text="Choose directory if you want to fetch info for all files inside a directory. You can specify certain filetypes.")
infodirectory.grid(row=2, column=0)
blank8 = Label(infowindow, text="")
blank8.grid(row=3, column=0)
infocsv = Label(infowindow,
text="Choose csv if you have a '.csv' file with the filenames. The tool will only return the filepath and filesize for the filenames provided in the csv.")
infocsv.grid(row=4, column=0)
def open_path():
path = filedialog.askdirectory(title="Select directory!")
show_path = Label(fetchfilesize, text="You chose this path: " + path, bg="#e4e5e5")
show_path.grid(column=0, row=11, columnspan=4)
blank = Label(fetchfilesize, text="", bg="#e4e5e5")
blank.grid(column=0, row=10, columnspan=4)
return path
def openfile():
file = filedialog.askopenfile(title="Select file!")
return file
def save_file():
location = filedialog.askdirectory(title="Save File!")
show_location = Label(fetchfilesize, text="Your file (output.xlsx) can be found here: " + location, bg="#e4e5e5")
show_location.grid(column=0, row=12, columnspan=4)
return location
def choose():
choice = [variable.get(), variable2.get()]
if tuple(choice) == ('', 'directory'):
def start():
column = ["filename", 'path', "filesize (MB)"]
_list = []
totalsize = 0
filetypes = [var1.get(), var2.get(), var3.get(), var4.get(), var5.get(), var6.get(), var7.get(), var8.get(),
var9.get()]
while "" in filetypes:
filetypes.remove("")
messagebox.showinfo("You chose filetype(s): ", filetypes)
for x, y, z in os.walk(open_path()):
for a in z:
if a.endswith(tuple(filetypes)):
b = os.path.join(x, a)
c = os.path.getsize(b)
d = round(c / (1024 * 1024), 3)
totalsize += d
e = str(a) + '%£~' + str(b) + '%£~' + str(d)
f = e.split('%£~')
_list.append(f)
df = pd.DataFrame(_list, columns=column)
messagebox.showinfo("Choose location", "Please choose a location to store the result")
# Windows OS
df.to_excel(str(save_file()) + "\output.xlsx")
# linux - MAC OS
df.to_excel(str(save_file()) + "/output.xlsx")
size = Label(fetchfilesize, text="The total size is: " + str(round(totalsize / 1024, 2)) + " GB",
bg="#e4e5e5")
size.grid(row=14, column=0, columnspan=4)
if len(_list) > 0:
averagesize = totalsize / len(_list)
average = Label(fetchfilesize, text="The average size is: " + str(round(averagesize, 2)) + " MB",
bg="#e4e5e5")
average.grid(row=15, column=0, columnspan=4)
else:
average = Label(fetchfilesize, text='The average size is: 0.0 MB', bg="#e4e5e5")
average.grid(row=15, column=0, columnspan=4)
var1 = StringVar()
var2 = StringVar()
var3 = StringVar()
var4 = StringVar()
var5 = StringVar()
var6 = StringVar()
var7 = StringVar()
var8 = StringVar()
var9 = StringVar()
checktif = Checkbutton(fetchfilesize, text=".tif", bg="#e4e5e5", onvalue=".tif", offvalue="", variable=var1)
checkjpg = Checkbutton(fetchfilesize, text=".jpg", bg="#e4e5e5", onvalue=".jpg", offvalue="", variable=var2)
checkwav = Checkbutton(fetchfilesize, text=".wav", bg="#e4e5e5", onvalue=".wav", offvalue="", variable=var3)
checkmov = Checkbutton(fetchfilesize, text=".mov", bg="#e4e5e5", onvalue=".mov", offvalue="", variable=var4)
checkmp3 = Checkbutton(fetchfilesize, text=".mp3", bg="#e4e5e5", onvalue=".mp3", offvalue="", variable=var5)
checkmp4 = Checkbutton(fetchfilesize, text=".mp4", bg="#e4e5e5", onvalue=".mp4", offvalue="", variable=var6)
checktiff = Checkbutton(fetchfilesize, text=".tiff", bg="#e4e5e5", onvalue=".tiff", offvalue="", variable=var7)
checkJPG = Checkbutton(fetchfilesize, text=".JPG", bg="#e4e5e5", onvalue=".JPG", offvalue="", variable=var8)
checkTIF = Checkbutton(fetchfilesize, text=".TIF", bg="#e4e5e5", onvalue=".TIF", offvalue="", variable=var8)
checktif.grid(row=5, column=0)
checkjpg.grid(row=5, column=1)
checkwav.grid(row=5, column=2)
checkmov.grid(row=5, column=3)
checkmp3.grid(row=6, column=0)
checkmp4.grid(row=6, column=1)
checktiff.grid(row=6, column=2)
checkJPG.grid(row=6, column=3)
checkTIF.grid(row=6, column=4)
blank2 = Label(text="", bg="#e4e5e5")
blank2.grid(row=4, column=1)
blank3 = Label(text="", bg="#e4e5e5")
blank3.grid(row=7, column=1)
buttonstart = Button(fetchfilesize, text="Start", padx=50, pady=10, borderwidth=4, bg="#04a37b", command=start)
buttonstart.grid(row=8, column=1, columnspan=2)
else:
def start():
column = ["filename", 'path', "filesize (MB)"]
lijst = []
for x, y, z in os.walk(open_path()):
for a in z:
b = os.path.join(x, a)
c = os.path.getsize(b)
d = round(c / (1024 * 1024), 3)
e = str(a) + '%£~' + str(b) + '%£~' + str(d)
f = e.split('%£~')
lijst.append(f)
df = pd.DataFrame(lijst, columns=column)
messagebox.showinfo("Choose file", "Please open file with filenames (one column with header 'filename')")
filenames = pd.read_csv(open_file())
result = pd.merge(filenames, df, left_on='filename', right_on="filename", how='left')
messagebox.showinfo("Choose location", "Please choose a location to store the result")
result.to_excel(save_file() + r"\result.xlsx")
blank4 = Label(text="", bg="#e4e5e5")
blank4.grid(row=4, column=1)
_info = Label(text="Press Start", bg="#e4e5e5")
_info.grid(row=5, column=1)
blank5 = Label(text="", bg="#e4e5e5")
blank5.grid(row=6, column=1)
buttonstart = Button(fetchfilesize, text="Start", padx=50, pady=10, borderwidth=4, bg="#04a37b", command=start)
buttonstart.grid(row=7, column=1, columnspan=4)
fetchfilesize = Tk()
fetchfilesize.title("fetch filesize")
fetchfilesize.configure(bg="#e4e5e5")
fetchfilesize.geometry("900x550")
Info = Label(text="Please choose filesource and press start")
Info.grid(row=0, column=1)
blank6 = Label(text="", bg="#e4e5e5")
blank6.grid(row=2, column=1)
variable = StringVar()
variable2 = StringVar()
Filesource1 = Checkbutton(fetchfilesize, text="csv", variable=variable, onvalue="csv", offvalue="", bg="#e4e5e5")
Filesource2 = Checkbutton(fetchfilesize, text="directory", variable=variable2, onvalue="directory", offvalue="",
bg="#e4e5e5")
Filesource1.grid(row=1, column=1)
Filesource2.grid(row=1, column=2)
buttonchoose = Button(fetchfilesize, text="Start", padx=50, pady=10, borderwidth=4, bg="#04a37b", command=choose)
buttonchoose.grid(row=3, column=1, columnspan=2)
buttoninfo = Button(fetchfilesize, text="Info", borderwidth=4, bg="#b9bcbb", command=info)
buttoninfo.grid(row=1, column=0)
fetchfilesize.mainloop()