from tkinter import filedialog
import os
import sys
from shutil import copyfile, move
#ASK FOR OPEN BEGIN DIRECTORY
def open_begin_foldert(event=None):
Begin_Folder = filedialog.askdirectory(title = "Select begin directory")
E_name.delete(END, 0)
E_name.insert(0, Begin_Folder)
#ASK FOR OPEN SEARCH DIRECTORY
def open_search_foldert(event=None):
End_Folder = filedialog.askdirectory(title = "Select search directory")
E_name_destination.delete(END, 0)
E_name_destination.insert(0, End_Folder)
#GET BEGIN, END DIRECTORY AND PARRET FOR SEARCH
def show_result():
Begin_Folder = E_name.get()
End_Folder = E_name_destination.get()
patter = E_patern.get()
return ((Begin_Folder, End_Folder, patter))
#--------------------START WITH SEARCH FUNTION'S ---------------
#FUNCTION USE FOR SEARCH PATTER USED IN M_SEARCH FUNCTION UNDER
def search_pattern(patter, Begin_Folder, End_Folder):
for root, dirname, filename in os.walk(Begin_Folder):
for file in filename:
if file.endswith(patter) and (root != End_Folder):
total_path = root+'/'+file
head, tail = os.path.split(total_path)
L.insert(END, root+"/"+file)
#SEARCH FUNCTION ADD TO BUTTON SEARCH!
def m_search():
Begin_Folder, End_Folder, patter = show_result()
search_pattern(patter, Begin_Folder, End_Folder)
#--------------------END WITH SEARCH FUNTION'S ---------------
#--------------------START WITH COPY FUNTION'S ---------------
def copy_pattern(patter, Begin_Folder, End_Folder):
for root, dirname, filename in os.walk(Begin_Folder):
for file in filename:
if file.endswith(patter) and (root != End_Folder):
counter = 0
total_path = root+'/'+file
head, tail = os.path.split(total_path)
try:
copyfile(total_path, End_Folder+"/"+tail)
counter += 1
print("Now copy file", self.End_Folder+"/"+tail)
except:
print("File {} exists or somthing wrong with copy!".format(End_Folder+"/"+tail))
L.insert(END, "Copy was {} file's".format(counter))
def m_copy():
Begin_Folder, End_Folder, patter = show_result()
copy_pattern(patter, Begin_Folder, End_Folder)
#--------------------END WITH COPY FUNTION'S ---------------
#--------------------START WITH MOVE FUNTION'S ---------------
def move_pattern(patter, Begin_Folder, End_Folder):
counter = 0
for root, dirname, filename in os.walk(Begin_Folder):
for file in filename:
if file.endswith(patter) and os.path.isdir(End_Folder):
total_path = root+'/'+file
head, tail = os.path.split(total_path)
try:
move(total_path, End_Folder+"/"+tail)
counter += 1
print("Move file", End_Folder+"/"+tail)
except:
print("somthing wrong with copy {}!".format(End_Folder+"/"+tail))
L.insert(END, "Move was {} file's".format(counter))
def m_move():
Begin_Folder, End_Folder, patter = show_result()
move_pattern(patter, Begin_Folder, End_Folder)
#--------------------END WITH MOVE FUNTION'S ---------------
#--------------------START WITH DELETED FUNTION'S ---------------
def delete_pattern(patter, Begin_Folder, End_Folder):
counter = 0
for root, dirname, filename in os.walk(Begin_Folder):
for file in filename:
if file.endswith(patter) and (root != End_Folder):
total_path = root+'/'+file
head, tail = os.path.split(total_path)
try:
os.remove(total_path)
counter += 1
print("Remove file", End_Folder+"/"+tail)
except:
print("somthing wrong with copy {}!".format(End_Folder+"/"+tail))
L.insert(END, "Deleted {} file's".format(counter))
def m_deleted():
Begin_Folder, End_Folder, patter = show_result()
delete_pattern(patter, Begin_Folder, End_Folder)
#-------------------END WITH DELETED FUNTION'S ---------------
#TKINTER CONFIGURATION
root = Tk()
root.title("File search or delete")
root.resizable(0,0)
root.geometry("400x450+100+100")
#LABEL's
L_name = Label(root, text="Search_folder")
L_name.grid(row=0, column=0, sticky=W)
L_dest_name = Label(root, text="Destination_folder")
L_dest_name.grid(row=1, column=0)
L_pattern = Label(root, text="Entry pattern")
L_pattern.grid(row=2, column=0, sticky=W)
#ENTRY's
E_NAME_VALUE = StringVar(root)
E_name = Entry(root, textvariable=E_NAME_VALUE)
E_name.grid(row=0, column=1)
E_name.bind('<Button-1>', open_begin_foldert)
E_name_destination = Entry()
E_name_destination.grid(row=1, column=1)
E_name_destination.bind('<Button-1>', open_search_foldert)
E_patern = Entry()
E_patern.grid(row=2, column=1)
#BUTTON's
B_search = Button(root, text="Search", command=m_search)
B_search.grid(row=0, column=2)
B_copy = Button(root, text="Copy", command=m_copy)
B_copy.grid(row=0, column=3)
B_move = Button(root, text="Move", command=m_move)
B_move.grid(row=1, column=3)
B_delete = Button(root, text="Delete", command=m_deleted)
B_delete.grid(row=1, column=2)
#SCROLLBAR
yScroll = Scrollbar(root,orient=VERTICAL)
yScroll.grid(row=3, column=6, sticky=N+S)
#LABEL IS UNDER!
L = Listbox(root, height=20)
L.grid(row=3, column=0, sticky=N+S+E+W, columnspan=5)
L.config(yscrollcommand=yScroll.set)
yScroll.config(command=L.yview)
#Begin the magic!!!
if __name__ == "__main__":
root.mainloop()
Thanks. Some useful commands and python. gmail login
ОтговорИзтриване