今天用wxPython做了一个GUI程序,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有文件都计算,大大减少了md5的计算量),加入了多线程功能。

以下是其脚本版本(无需安装wxPython)

UNIQFile-script.py

 # -*- coding: gbk -*-

 '''
Author:@DoNotSpyOnMe
Blog: http://www.cnblogs.com/aaronhoo
''' import hashlib
import os
import threading def getFileSize(filePath):
return os.path.getsize(filePath) ''' 一般文件的md5计算方法,一次读取文件的全部内容'''
def CalcMD5(filepath):
with open(filepath,'rb') as f:
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
return hash
'''大文件计算md5的方法,分批读取文件内容,防止内存爆掉'''
def GetFileMd5(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = open(filename,'rb')
while True:
b = f.read(8*1024)
if not b :
break
myhash.update(b)
f.close()
return myhash.hexdigest() def GetAllFiles(directory):
files=[]
for dirpath, dirnames,filenames in os.walk(directory):
if filenames!=[]:
for file in filenames:
files.append(dirpath+'\\'+file)
     files.sort(key=len)#按照文件名的长度排序
return files def findSameSizeFiles(files):
dicSize={}
for f in files:
size=getFileSize(f)
if not dicSize.has_key(size):
dicSize[size]=f
else:
dicSize[size]=dicSize[size]+';'+f
dicCopy=dicSize.copy()
for k in dicSize.iterkeys():
if dicSize[k].find(';')==-1:
dicCopy.pop(k)
del dicSize
return dicCopy def findSameMD5Files(files):
dicMD5={}
for f in files:
print 'calculating the md5 value of file %s'%f
md5=GetFileMd5(f)
if not dicMD5.has_key(md5):
dicMD5[md5]=f
else:
dicMD5[md5]=dicMD5[md5]+';'+f
dicCopy=dicMD5.copy()
for k in dicMD5.iterkeys():
if dicMD5[k].find(';')==-1:
dicCopy.pop(k)
del dicMD5
return dicCopy def removeSameFile(mydir):
msg=''
msgUniq='Result:No file is removed since they are all uniq.'
try:
existsFlag=False
files=GetAllFiles(mydir)
print'%s files found in directory %s\n'%(len(files),mydir)
dicFileOfSameSize=findSameSizeFiles(files)
if dicFileOfSameSize=={}:
print msgUniq
return
else:
#list the duplicated files first:
dicFiltered={}
for k in dicFileOfSameSize.iterkeys():
filesOfSameSize=dicFileOfSameSize[k].split(';')
dicSameMD5file=findSameMD5Files(filesOfSameSize)
if dicSameMD5file!={}:
existsFlag=True
for k in dicSameMD5file.iterkeys():
msg=msg+'md5 %s: %s'%(k,dicSameMD5file[k])+'\n'
dicFiltered[k]=dicSameMD5file[k]
if not existsFlag:
msg=msgUniq
return
else:
msg='Duplicated files:\n'+msg+'\n'
#then remove the duplicated files:
removeCount=0
for k in dicFiltered.iterkeys():
sameFiles=dicFiltered[k].split(';')
flagRemove=False
for f in sameFiles:
if not flagRemove:
flagRemove=True
else:
msg=msg+'Removing file: %s'%f+'\n'
os.remove(f)
removeCount=removeCount+1
msg=msg+'%s files are removed.\n'%removeCount
except Exception,e:
print e
# msg='Exception occured.'
finally:
print msg+'\n'+'Operation finished.' def listSameFile(mydir):
msg=''
msgUniq='Result:All files are uniq.'
try:
existsFlag=False
files=GetAllFiles(mydir)
print '%s files found in directory %s\n'%(len(files),mydir)
dicFileOfSameSize=findSameSizeFiles(files)
if dicFileOfSameSize=={}:
print msgUniq
return
else:
for k in dicFileOfSameSize.iterkeys():
filesOfSameSize=dicFileOfSameSize[k].split(';')
dicSameMD5file=findSameMD5Files(filesOfSameSize)
if dicSameMD5file!={}:
existsFlag=True
for k in dicSameMD5file.iterkeys():
msg=msg+'md5 %s: %s'%(k,dicSameMD5file[k])+'\n'
if not existsFlag:
msg=msgUniq
else:
msg='Duplicated files:\n'+msg
except Exception,e:
print e
# msg='Exception occured.'
finally:
print msg+'\n'+'Operation finished.' if __name__=="__main__":
print 'This program is designed for clearing the duplicated files and saving memory space.Select a directory and we will find or remove the duplicated files.'
print 'All rights are reserved by @DoNotSpyOnMe'
print '\n' print "You have three options:"
print "'f' for finding the duplicated files in the directory that you're required to enter later,or"
print "'r' for finding and the removing the duplicated file,or"
print "'q' to quit"
while True:
option=raw_input('Please enter your option:\n')
option=option.lower()
while option!='f' and option!='r' and option!='q':
option=raw_input('Please enter your option:\n')
if option=='f' or option=='r':
mydir=raw_input('Please enter the direcotry containing files:\n')
mydir=mydir.lower()
while mydir.find('\\')==-1 or not os.path.isdir(mydir):
mydir=raw_input('Please enter a valid direcotry containing files:\n')
if option=='f':
listSameFile(mydir)
else:
removeSameFile(mydir)
elif option=='q':
exit(0)
print ''

python查找并删除相同文件-UNIQ File-script版本的更多相关文章

  1. python查找并删除相同文件-UNIQ File-wxPython版本

    今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...

  2. python查找并删除相同文件-UNIQ File-wxPython-v6

    相比第一版,新增:菜单,对话框,文件过滤器,操作结果保存,配置功能(自己写了一个读写配置文件的功能),提示语优化,模块分化更合理. 截图: 源代码: UniqFile-wxPython-v6.py: ...

  3. Linux查找并删除重复文件的命令行fdupes工具,dupeGuru图形工具

    查了几十个网页,找到这个接近满意的解决方案http://unix.stackexchange.com/questions/146197/fdupes-delete-files-aft... 不过正则里 ...

  4. Linux系统中查找、删除重复文件,释放磁盘空间。

    在Linux系操作系统中查找并删除重复文件的方法的确有很多,不过这里介绍的是一款非常简单实用的软件FSlint.FSlint是一个重复文件查找工具,可以使用它来清除不必要的重复文件,笔者经常使用它来释 ...

  5. Python小工具--删除svn文件

    有的时候我们需要删除项目下的svn相关文件,但是SVN会在所有的目录下都创建隐藏文件.svn,手工一个个目录查找然后删除显然比较麻烦.所以这里提供了一个Python小工具用于批量删除svn的相关文件: ...

  6. 利用lsof命令查找已经删除的文件来释放磁盘空间

    测试环境一台服务器/目录空间使用率达到97%,但是通过du -sh *发现实际空间没用到那么多,初步怀疑,之前删除的文件,有运行中的进程一直占用,导致空间没有释放,如图通过du -sh *发现共实际使 ...

  7. 在 Linux 中查找和删除重复文件

    原文链接:https://www.linuxprobe.com/linux-FSlint.html FSlint同时具有GUI和CLI模式.因此,对于新手来说,这是一个用户友好的工具.FSlint不仅 ...

  8. fso查找被删除的文件

    <html> <head> </head> <body> 源目录:<input id="txtOld" value=" ...

  9. python 递归删除空文件夹

    Python如何递归删除空文件夹 1.Python如何递归删除空文件夹,这个问题很常见.但大多数人的解决办法都是自己实现递归函数解决这个问题,其实根本不用那么麻烦.Python中的os.walk提供了 ...

随机推荐

  1. ecshop去掉“云服务中心”或者是“模板堂知识库”

    ECSHOP开发中心(www.68ecshop.com)教程介绍一下如何去除后台云服务中心菜单: 打开admin/templates/menu.htm,把415行的 document.getEleme ...

  2. wex5 开机图片时间长

    作用: 控制刚打开图片 时间长 修改config.xml  地址:F:\wex\model\Native\templates\advanced 延迟的时间是在本地app的 config.xml中修改, ...

  3. background属性

    background: url(images/01.jpg) 0 10px; 效果: background: url(images/01.jpg) 0 -10px; 效果: *注释:10px 是网上去 ...

  4. <?xml version="1.0" encoding="utf-16"?>. use different encoding

    public string Serialize<T>(T serializeClass) { string xmlString = string.Empty; try { if (seri ...

  5. msbuild

    https://msdn.microsoft.com/zh-cn/library/ms171466(v=vs.100).aspx

  6. vs2013使用C#6.0

    安装 nuget Microsoft.Net.Compilers

  7. android控件库(1)-带删除功能的EditText

    DJEditText.java /** * Created by xp.chen on 2016/11/25. */ public class DJEditText extends AppCompat ...

  8. visio2007使用记录

    安装时, 需要有选择性的安装, 不是所有的组件 都需要安装, 如office工具, office共享功能, visio中的.net可编程支持就用不着安装.... visio中的cad(加载项), 可以 ...

  9. iframe和frameset的使用

    iframe是:inner frame的缩写, 必须指明src属性,不能直接在里面写内容, 否则不会显示. 可以载入html, *.图片文件, txt文件等等. html的属性中,表示长度高度的尺寸属 ...

  10. Web渗透测试使用Kali Linux(一)渗透测试概要及环境部署

    渗透测试是利用已经发现的漏洞,采用恶意黑客的惯用手段来尝试对漏洞进行攻击. Kali Linux是BackTrack的进化版,是Linux的衍生版本,专门开发用作渗透测试,其中提供了很多的渗透测试工具 ...