-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.py
More file actions
295 lines (266 loc) · 14.6 KB
/
FileManager.py
File metadata and controls
295 lines (266 loc) · 14.6 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from distutils.dir_util import copy_tree
from tkinter import *
import os
import shutil
from tkinter import ttk, filedialog, messagebox, simpledialog
from datetime import datetime
class FileManager:
def __init__(self, master):
self.frame_header = ttk.Frame(master)
self.frame_header.grid(row=0, columnspan=2)
self.frame_changelog = ttk.Frame(master)
self.frame_changelog.grid(row=1, columnspan=2)
self.frame_buttons = ttk.Frame(master)
self.frame_buttons.grid(row=2, column=0)
self.frame_files_name_type = ttk.Frame(master)
self.frame_files_name_type.grid(row=2, column=1)
self.frame_footer = ttk.Frame(master)
self.frame_footer.grid(row=3, column=1, columnspan=2)
ttk.Label(self.frame_header, text='This script will help you manage files on your computer.').grid(row=0,
column=1)
ttk.Label(self.frame_buttons, text='Choose your action:').grid(row=1, column=0, pady=(0, 15))
ttk.Label(self.frame_footer, text='\t\tby Bartosz Marmołowski', font=('Arial', 7)).grid(row=0, column=1)
self.changelog_field = Text(self.frame_changelog, width=70, height=10, font=('Arial', 7), wrap=WORD)
self.changelog_field.grid(row=0, column=0, columnspan=2, padx=(5, 0), pady=5)
self.changelog_scrollbar = ttk.Scrollbar(self.frame_changelog, command=self.changelog_field.yview)
self.changelog_scrollbar.grid(row=0, column=2, sticky='nse')
self.changelog_field['yscrollcommand'] = self.changelog_scrollbar.set
self.changelog_field.config(state=DISABLED)
self.copy_file_button = ttk.Button(self.frame_buttons, text='Copy File', command=self.copy_file)
self.copy_file_button.grid(row=2, sticky='ew', padx=10)
self.delete_file_button = ttk.Button(self.frame_buttons, text='Delete File', command=self.delete_file)
self.delete_file_button.grid(row=3, sticky='ew', padx=10)
self.rename_file_button = ttk.Button(self.frame_buttons, text='Rename File', command=self.rename_file)
self.rename_file_button.grid(row=4, sticky='ew', padx=10)
self.move_file_button = ttk.Button(self.frame_buttons, text='Move File', command=self.move_file)
self.move_file_button.grid(row=5, sticky='ew', padx=10)
self.make_folder_button = ttk.Button(self.frame_buttons, text='Create Folder', command=self.create_folder)
self.make_folder_button.grid(row=6, sticky='ew', padx=10)
self.copy_folder_button = ttk.Button(self.frame_buttons, text='Copy Folder', command=self.copy_folder)
self.copy_folder_button.grid(row=7, sticky='ew', padx=10)
self.delete_folder_button = ttk.Button(self.frame_buttons, text='Delete Folder')
self.delete_folder_button.grid(row=8, sticky='ew', padx=10)
self.list_files_button = ttk.Button(self.frame_buttons, text='List all Files in Folder',
command=self.list_files_in_dir)
self.list_files_button.grid(row=9, sticky='ew', ipadx=10, padx=10)
self.list_files_button = ttk.Button(self.frame_buttons, text='Export Actions Log')
self.list_files_button.grid(row=10, sticky='ew', ipadx=10, padx=10)
self.frame_file_list = Frame(self.frame_files_name_type)
self.frame_file_list.grid(row=1, column=0)
self.file_name_list_field = Text(self.frame_file_list, width=25, height=25, wrap=NONE, font=('Arial', 7))
self.file_name_list_field.grid(row=1, column=0)
self.file_name_list_field.config(state=DISABLED)
self.file_type_field = Text(self.frame_file_list, width=12, height=25, wrap=NONE, font=('Arial', 7))
self.file_type_field.grid(row=1, column=1)
self.file_type_field.config(state=DISABLED)
self.file_list_scrollbar = Scrollbar(self.frame_file_list)
self.file_list_scrollbar.grid(row=1, column=2, sticky='nsew')
self.file_list_scrollbar['command'] = self.scrollbar_func
self.file_name_list_field['yscrollcommand'] = self.text_scroll_func
self.file_type_field['yscrollcommand'] = self.text_scroll_func
self.file_name_scrollbar = Scrollbar(self.frame_file_list, orient='horizontal',
command=self.file_name_list_field.xview)
self.file_name_scrollbar.grid(row=2, column=0, sticky='we')
self.file_name_list_field['xscrollcommand'] = self.file_name_scrollbar.set
self.file_type_scrollbar = Scrollbar(self.frame_file_list, orient='horizontal',
command=self.file_type_field.xview)
self.file_type_scrollbar.grid(row=2, column=1, sticky='we')
self.file_type_field['xscrollcommand'] = self.file_type_scrollbar.set
def scrollbar_func(self, *args):
self.file_name_list_field.yview(*args)
self.file_type_field.yview(*args)
def text_scroll_func(self, *args):
self.file_list_scrollbar.set(*args)
self.scrollbar_func('moveto', args[0])
def copy_file(self):
original_location = filedialog.askopenfilename(
initialdir=os.getcwd(),
title="Select a file to copy"
)
if not original_location:
return
copied_file_name = os.path.split(original_location)[1]
original_location_name = os.path.split(original_location)[0]
destination_location = filedialog.askdirectory(
title='Choose destination'
)
if not destination_location:
return
potential_destination = os.path.join(destination_location, copied_file_name)
if original_location_name == destination_location:
messagebox.showerror(title='Same location!',
message='Chosen destination is the same as original file location!')
return
if os.path.exists(potential_destination):
question = messagebox.askyesno(title='File already exists!',
message='Do you wish to overwrite existing file with the same name?')
if not question:
return
shutil.copy(original_location, destination_location)
messagebox.showinfo(title='File copied.', message='File copied successfully!')
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
self.changelog_field.config(state=NORMAL)
self.changelog_field.insert('0.0',
f'[{current_time}] Copied file {copied_file_name} from '
f'{original_location_name} to {destination_location}.\n\n')
self.changelog_field.insert('0.0', 'COPY FILE operation:\n')
self.changelog_field.config(state=DISABLED)
def delete_file(self):
file_location = filedialog.askopenfilename(
initialdir=os.getcwd(),
title="Select a file to delete"
)
if not file_location:
return
question = messagebox.askyesno(title='Confirmation required.',
message='Are you sure you want to delete this file?')
if not question:
return
os.remove(file_location)
messagebox.showinfo(title='File deleted.', message='File deleted successfully!')
deleted_file_name = os.path.split(file_location)[1]
deleted_file_location = os.path.split(file_location)[0]
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
self.changelog_field.config(state=NORMAL)
self.changelog_field.insert('0.0',
f'[{current_time}] Deleted file {deleted_file_name} from '
f'{deleted_file_location}.\n\n')
self.changelog_field.insert('0.0', 'DELETE FILE operation:\n')
self.changelog_field.config(state=DISABLED)
def rename_file(self):
file_location = filedialog.askopenfilename()
if not file_location:
return
file_location_dir = os.path.dirname(file_location)
extension = os.path.splitext(file_location)[1]
original_file = os.path.split(file_location)[1]
original_file_name = os.path.splitext(original_file)[0]
new_filename = simpledialog.askstring(title='New file name.', prompt='Enter new file name:')
if new_filename is None:
return
elif new_filename == '':
messagebox.showerror(title='File name error!', message='No new name was provided!')
return
elif original_file_name == new_filename:
messagebox.showerror(title='File name error!', message='New file name is the same as the original one!')
return
else:
question = messagebox.askyesno(title='Confirmation required.',
message=f'Are you sure you want to rename your file to \'{new_filename}\'?')
if not question:
return
renamed_file_location = os.path.join(file_location_dir, new_filename + extension)
os.rename(file_location, renamed_file_location)
messagebox.showinfo(title='File renamed.', message='File renamed successfully!')
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
self.changelog_field.config(state=NORMAL)
self.changelog_field.insert('0.0',
f'[{current_time}] Renamed file {original_file} to '
f'{new_filename + extension} inside {file_location_dir}.\n\n')
self.changelog_field.insert('0.0', 'RENAME FILE operation:\n')
self.changelog_field.config(state=DISABLED)
def move_file(self):
original_location = filedialog.askopenfilename(
initialdir=os.getcwd(),
title="Select a file to move"
)
if not original_location:
return
moved_file_name = os.path.split(original_location)[1]
original_location_name = os.path.split(original_location)[0]
destination_location = filedialog.askdirectory(
title='Choose destination'
)
if not destination_location:
return
potential_destination = os.path.join(destination_location, moved_file_name)
if original_location_name == destination_location:
messagebox.showerror(title='Same location!',
message='Chosen destination is the same as original file location!')
return
if os.path.exists(potential_destination):
question = messagebox.askyesno(title='File already exists!',
message='Do you wish to overwrite existing file with the same name?')
if not question:
return
shutil.move(original_location, destination_location)
messagebox.showinfo(title='File moved.', message='File moved successfully!')
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
self.changelog_field.config(state=NORMAL)
self.changelog_field.insert('0.0',
f'[{current_time}] Moved file {moved_file_name} from '
f'{original_location_name} to {destination_location}.\n\n')
self.changelog_field.insert('0.0', 'MOVE FILE operation:\n')
self.changelog_field.config(state=DISABLED)
def create_folder(self):
new_folder_location = filedialog.askdirectory()
if not new_folder_location:
return
new_folder_name = simpledialog.askstring(title='Folder name.',
prompt='Enter folder name that you would like to create:')
new_folder = os.path.join(new_folder_location, new_folder_name)
if os.path.exists(new_folder):
messagebox.showerror(title='Folder exists!',
message=f'Folder with that name already exists in {new_folder_location}')
else:
os.mkdir(new_folder)
messagebox.showinfo(title='Folder created.', message='Folder created successfully!')
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
self.changelog_field.config(state=NORMAL)
self.changelog_field.insert('0.0',
f'[{current_time}] Created folder {new_folder_name} in '
f'{new_folder_location}.\n\n')
self.changelog_field.insert('0.0', 'CREATE FOLDER operation:\n')
self.changelog_field.config(state=DISABLED)
def copy_folder(self):
folder_to_copy = filedialog.askdirectory()
if not folder_to_copy:
return
folder_destination = filedialog.askdirectory()
if not folder_destination:
return
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
try:
folder_destination_name = os.path.join(folder_destination, os.path.basename(folder_to_copy))
os.mkdir(folder_destination_name)
copy_tree(folder_to_copy, folder_destination_name)
except FileExistsError:
folder_destination_name = os.path.join(folder_destination, os.path.basename(folder_to_copy + ' - copy'))
os.mkdir(folder_destination_name)
copy_tree(folder_to_copy, folder_destination_name)
self.changelog_field.config(state=NORMAL)
self.changelog_field.insert('0.0',
f'[{current_time}] Copied folder {folder_to_copy} into '
f'{folder_destination}.\n\n')
self.changelog_field.insert('0.0', 'COPY FOLDER operation:\n')
self.changelog_field.config(state=DISABLED)
def list_files_in_dir(self):
folderList = filedialog.askdirectory()
if not folderList:
return
self.file_name_list_field.config(state=NORMAL)
self.file_type_field.config(state=NORMAL)
self.file_name_list_field.delete(1.0, END)
self.file_type_field.delete(1.0, END)
for file in os.listdir(folderList):
file_name = os.path.splitext(file)[0]
file_type = os.path.splitext(file)[1]
self.file_name_list_field.insert(END, file_name + '\n')
if file_type == '':
self.file_type_field.insert(END, 'folder' + '\n')
else:
self.file_type_field.insert(END, file_type + '\n')
self.file_name_list_field.config(state=DISABLED)
self.file_type_field.config(state=DISABLED)
def main():
root = Tk()
root.title('FileManager')
file_manager = FileManager(root)
root.mainloop()
if __name__ == "__main__": main()