未加星标

Remove the duplicate file from nested folders with python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
In this article we will continue to develop our python application which will remove the duplicate file within a folder. In the last chapter we have removed a duplicate file in another folder and this time we will remove all the duplicate files within the nested folders by slightly modifying the previous program. First of all we will edit the main file by replacing the forward slash with backward slash to suite the windows file path’s format. 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(): 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 remove thread to delete the duplicate file if(folder != ''): folder = folder.replace('/', '\\') remove = Remove(folder, aLabel, filename) remove.start() # 8 Adding a Button action = Button(win, text="Select File", 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 Next we will modify the remove class so now we can remove all the duplicate files within nested 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): filepaths = os.listdir(self.massage) for filepath in list(filepaths): os.chdir(self.massage) if(os.path.isfile(filepath)): if(filepath == self.filename): os.remove(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) # need this to reset the current folder if(os.path.isfile(filepath)): if (filepath == self.filename): os.remove(filepath) else: self.delete_duplicate(os.path.join(folder, filepath)) After you have selected a file from a folder and selected another folder which you want to search and remove the file with the same file name as the one inside the previous folder, you can just sit back and wait for the program to search and destroy all the duplicate files in that second folder plus all the duplicate files inside the folders that are within the second folder as well as the duplicate file insides the folder which contains inside the folder which is insides that second folder. The program has successfully removed all the duplicate files within folders that have less than 50 files each without any delays with only one new thread. Will the program slow down if there are lots of folders and files to search for? We don’t know yet and will only modify it if we need more threads to handle the job. Our next goal is to remove the file if and only if the content of that file is the same as the selected one. Remember what I have written in the previous chapter? We simply cannot assume that two files with the same name and the same file’s extension contain the same content. So stay tune for the next chapter.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Remove the duplicate file from nested folders with python
本站链接:https://www.codesec.net/view/620944.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(163)