Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268908
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Python/Ruby

2013-05-03 11:36:17

以MD5校验和的方式比较文件,遍历目录,将文件放入record={},在遍历过程中如果发现有相同的文件,则将相同的文件放入dup=[]

import hashlib
def checksum(file):
  fp=open(file)
  checksum=hashlib.md5()
  while True:
    buffer=fp.read(8192)
    if not buffer:break
    checksum.update(buffer)
  fp.close()
  checksum=checksum.digest()
  return checksum

import os
def diskwalk(path):
  fullpath=[]
  for paths,dirs,files in os.walk(path):
    for file in files:
      filepath=os.path.join(paths,file)
      fullpath.append(filepath)
  return fullpath

def getsize(file):
  size=os.stat(file)[6]
  return size

path='/opt/python'
files=diskwalk(path)
dup=[]

for file in files:
  compound_key=(getsize(file),checksum(file))
  if compound_key in record:
    dup.append(file)
  else:
    record[compound_key]=file
print  record
print "###############"
print dup

阅读(1251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~