一、简介:
实现方式为使用哈希值对比文件是否重复,应用场景有越积越大的下载空间。
二、界面:
三、部分源码:
1.需要的包:
import os#操作文件夹及删除文件需要
import hashlib#哈希计算需要
#import binascii#转16进制需要,当然可以不转
from tkinter import *#UI需要
from tkinter import messagebox#弹个窗
from tkinter import filedialog#选择路径需要
import tkinter.font as tkFont#字体设置
import tkinter.ttk#进度条需要
2.UI实现:
window = Tk()# 创建主窗口
#window.attributes("-topmost",1)#置顶
ft = tkFont.Font(family='SourceHanSansCN-Bold',size=8)#设置字体和字号
window.title('重复文件删除工具')# 窗口命名为归档工具
screenWidth = window.winfo_screenwidth() # 获取显示区域的宽度
screenHeight = window.winfo_screenheight() # 获取显示区域的高度
width = 500# 设定窗口宽度
height = 250# 设定窗口高度
left = (screenWidth - width) / 2#距左
top = (screenHeight - height) / 2#距上
window.geometry("%dx%d+%d+%d" % (width,height,left,top))#设置窗口为720*480且居中初始化
window.resizable(width=False, height=False)#不可调整大小
#window.iconbitmap('HASHlogo.ico')# 更改窗口图标
#window['background']='green'#设置背景颜色
window.attributes("-alpha",0.8)# 窗口透明度
Label(window,text = "version:RC 1.0 ©wUw\nwuwofcproject.com\nwww.yuque.com/u2205738",font=ft).place(relx = 0.35,rely = 0.8)
def select_folder1():
# 单个文件选择
selected_folder1 = filedialog.askdirectory() # 选择路径
varF_1.set(selected_folder1)
varF_1 = StringVar()
LF_1 = Label(window,text = "选择整理路径:",font=ft).place(relx = 0.05,rely = 0.15)
EnF_1 = Entry(window, textvariable = varF_1).place(relx = 0.21,width = 280,rely = 0.15)
Button(window, text="选择路径", command=select_folder1,font=ft).place(relx =0.78,width = 80,rely = 0.15)
btn = Button(window,text = "去重",font=ft,command = lambda:[delfile()]).place(relx = 0.88,rely = 0.8)
btn = Button(window,text = "查重",font=ft,command = lambda:[inspect()]).place(relx = 0.78,rely = 0.8)
progressbarTwo = tkinter.ttk.Progressbar(window) # 设置进度条
progressbarTwo.place(relx=0.05, width=450, height=7, rely=0.65) # 设置进度条长宽高
progressbarOne = tkinter.ttk.Progressbar(window) # 设置进度条
progressbarOne.place(relx=0.05, width=450, height=7, rely=0.55) # 设置进度条长宽高
window.mainloop()#保持窗口
3.大文件划块计算哈希值:
with open(listA[i], 'rb', buffering=0) as f: # 二进制读取文件
print(i)
sha224 = hashlib.sha224() # 哈希 sha224
while True:
IV00 = f.read(8192) # 一次读取8192,分段读取,直至读取完毕
sha224.update(IV00) # 拼接
# print(IV00)
if not IV00:
break
# print(sha224.hexdigest())
listB.append(sha224.hexdigest()) # 将sha224哈希值写入listB
progressbarOne['value'] += 1 # 进度值+1
window.update()
4.哈希值对比:
# 开始对比文件哈希值是否相同↓
num0 = 0
while num0 < len(listB):
for x in range(num0 + 1, len(listB)):
# print('.', x)
if listB[num0] == listB[x]:
listD.append(listA[x])
num0 += 1
listD = list(set(listD))
# 对比到此结束,并去除重复↑
5.删除文件及留痕:
# 将去除的文件写入txt↓
file = open(varF_1.get() + '\\del flie.txt', 'w')
file.write('\n'.join(listD))
# 删除文件↓
for z in listD:
if os.path.exists(z):
os.remove(z)
progressbarTwo['value'] = 2
messagebox.showinfo("通知(Tips)", "完成!")#弹出结束通知
os.startfile(varF_1.get() + '\\del flie.txt')#打开文件