Welcome to the new chapter of this remove duplicate file project. After creating a thread class, open a file and open a folder in the previous few chapters, we can now start to move toward our main objective which is to search for a duplicate file in another folder.
What we will do in this chapter is to write a program to select a file and select a folder which we want to look for the file with the same filename as the one which we have just selected earlier. If the file with the same name does exists in another folder then we will print the file found statement on the application’s label or else we will print out no file with the same name found statement on that same application’s label.
Lets get started. First of all we will edit the main program file to select a file and the folder, the open folder dialog will only open if and only if a file has been selected.
from tkinter import * from tkinter import filedialog from Remove import Remove win = Tk() # 1 Create instance win.title("Multitas") # 2 Add a title win.resizable(0, 0) # 3 Disable resizing the GUI win.configure(background='black') # 4 change background color # 5 Create a label aLabel = Label(win, text="Remove duplicate file", anchor="center") aLabel.grid(column=0, row=1) aLabel.configure(foreground="white") aLabel.configure(background="black") # 6 Create a selectFile function to be used by button def selectFile(): filename = filedialog.askopenfilename(initialdir="/", title="Select file") if(filename != ''): filename = filename.split('/')[-1] # this is for the windows separator only folder = filedialog.askdirectory() # 7 open a folder then create and start a new thread to print those filenames from the selected folder remove = Remove(folder, aLabel, filename) remove.start() # 8 Adding a Button action = Button(win, text="Open Folder", command=selectFile) action.grid(column=0, row=0) # 9 Position the button action.configure(background='brown') action.configure(foreground='white') win.mainloop() # 10 start GUI
Now we will modify our Remove thread class to search for a file inside another folder.
import threading import os class Remove(threading.Thread): def __init__(self, massage, aLabel, filename): threading.Thread.__init__(self) self.massage = massage self.label = aLabel self.filename = filename def run(self): text_filename = 'There is no duplicate item' filepaths = os.listdir(self.massage) for filepath in filepaths: if(filepath == self.filename): text_filename = 'Found duplicate item' self.label.config(text=text_filename) return
If you run the above program you will see this statement appears if the duplicate file has been found!
Found the duplicate itemIn the next chapter we will start to remove the duplicate file from another folder!
from Planet Python
via read more
No comments:
Post a Comment