Sunday, December 9, 2018

codingdirectional: Create multiple threads to delete multiple files with python

In this article we are going to continue with the previous remove duplicate project and this time the program will create multiple threads to remove multiple duplicate files in the hard drive of the windows os’s laptop. Before we can remove multiple duplicate files in the pc hard drive we will need to select multiple files that will be used by the program to search for the multiple duplicate files in another folder of our laptop hard drive first,  in order to select multiple files in windows hard drive we will need to left click on one file and then hold shift and left click on the other files at the same time.

Now lets edit the main project file to make some changes. We will first remove the label parameter from the remove thread because it has served no purpose anymore in our program. Next we will use the filedialog.askopenfilenames method to open the file dialog which allows us to select multiple files at the same time. The last thing we need to do is to create separate thread to handle separate duplicate file removal process, how many threads can we actually create at the same time is depends on the memory limit of our computer, but you do not need to worry about this issue because the search and destroy process only takes very short time, thus one thread will be freed and ready to handle the next round of search and destroy process after it has finished it’s previous job in a very short time period. If you want to know how many threads can the cpu core produces then here is a great article about this topic.

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", 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():

    fullfilenames = filedialog.askopenfilenames(initialdir="/", title="Select file") # select multiple files from the hard drive
    fullfilenamelist = win.tk.splitlist(fullfilenames)

    if(fullfilenamelist[0] != ''):

        folder = filedialog.askdirectory()  # 7 open a folder then create and start a new remove thread to delete the duplicate file

        if(folder != ''):
            folder = folder.replace('/', '\\')
            for fullfilename in fullfilenamelist:

                if(fullfilename != ''):
                    filename = fullfilename.split('/')[-1] # 8 this is for the windows separator only
                    remove = Remove(folder, filename, fullfilename)
                    remove.start()
                    remove.join()

# 9 Adding a Button
action = Button(win, text="Select File", command=selectFile)
action.grid(column=0, row=0)
action.configure(background='brown')
action.configure(foreground='white')

win.mainloop()  # 10 start GUI

Nothing much for us to modify in the remove thread class, all we need to do is just to remove the label parameter which we no longer need it.

import threading
import os
import filecmp

class Remove(threading.Thread):

   def __init__(self, massage,  filename, fullfilename):

      threading.Thread.__init__(self)
      self.massage = massage
      self.filename, self.file_extension = os.path.splitext(filename)
      self.fullfilename = fullfilename

   def run(self):

      filepaths = os.listdir(self.massage)

      for filepath in list(filepaths):
         os.chdir(self.massage)
         if(os.path.isfile(filepath)):
            filename, file_extension = os.path.splitext(filepath)
            self.remove_file(file_extension, filepath)
         else:
            self.delete_duplicate(os.path.join(self.massage, filepath))
      return

   def delete_duplicate(self, folder): # sub method to pass folder to

      filepaths = os.listdir(folder)

      for filepath in list(filepaths):
         os.chdir(folder)
         if(os.path.isfile(filepath)):
            filename, file_extension = os.path.splitext(filepath)
            self.remove_file(file_extension, filepath)
         else:
            self.delete_duplicate(os.path.join(folder, filepath))

   def remove_file(self, file_extension, filepath):
      if (file_extension == self.file_extension):
         if filecmp.cmp(filepath, self.fullfilename, shallow=False):
            os.remove(filepath)

That is it, now we will have a fully function able multiple files selection remove duplicate files program!  I will continue use this program to search for lots of files and see whether will the program slows down or not when we search for lots of files at the same time. The program next task is to only allows the search process to begin if and only if the user has selected a different folder instead of a current one because we certainly don’t want to remove the file which is in the present folder, so stay tune for the next lesson.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...