加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python删除同一个文件夹下的重复文件代码

发布时间:2020-05-24 23:47:02 所属栏目:Python 来源:互联网
导读:Python删除同一个文件夹下的重复文件代码

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

#!/usr/bin/envpython
#coding:utf-8

importmd5
importos
fromtimeimportclockasnow


defgetmd5(filename):
file_txt=open(filename,'rb').read()
m=md5.new(file_txt)
returnm.hexdigest()


defmain():
path=u'C:aabbccdd'
all_md5={}
all_size={}
total_file=0
total_delete=0
start=now()
forfileinos.listdir(path):
total_file+=1
real_path=os.path.join(path,file)
ifos.path.isfile(real_path)==True:
size=os.stat(real_path).st_size
name_and_md5=[real_path,'']
ifsizeinall_size.keys():
new_md5=getmd5(real_path)
ifall_size[size][1]=='':
all_size[size][1]=getmd5(all_size[size][0])
ifnew_md5inall_size[size]:
total_delete+=1
printu'删除',file
try:
os.remove(os.path.join(path,file))
except:
print'Nosuchfile:%s'%file
else:
all_size[size].append(new_md5)
else:
all_size[size]=name_and_md5
end=now()
time_last=end-start
printu'文件总数:',total_file
printu'删除个数:',total_delete
printu'耗时:',time_last,'秒'


if__name__=='__main__':
main()
try:
os.remove(os.path.join(path,file))
except:
print'Nosuchfile:%s'%file

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读