添加两个目录,依据目录中递归搜索所有的文件,对比两个目录中文件名相同的文件,产生[DIFF]表示有差异的问题,[DEL]表示在A中删掉的文件,[NEW]表示A中新建的文件
目前存在的不足:若目录递归中存在相同的文件名,后面的文件路径将覆盖之前的,也就是只统计一次
代码如下:
# -*- coding: utf-8 -*-
import os
#!/usr/bin/env python
import os
import sys
import hashlib
class node(object):
def __init__(self, name, dir):
self.name = name
self.dir = dir
def get_list(path):
mydic = {}
count = 0
for root, dirs, files in os.walk(path):
for fileName in files:
# filePathsA[(root + “\\” + fileName).lower()] = 1;
if fileName == ‘.DS_Store’:
continue
print root+’/’+fileName
mydic[fileName] = root+’/’+fileName
count = count + 1
print count
return mydic
def get_diff(Adir, Bdir):
count = 0
for Akey in Adir.keys():
if Bdir.has_key(Akey):
f1 = open(Adir[Akey], ‘rb’)
f2 = open(Bdir[Akey], ‘rb’)
Amac = hashlib.md5(f1.read())
Bmac = hashlib.md5(f2.read())
if Amac.hexdigest() != Bmac.hexdigest():
count = count + 1
print Amac.hexdigest() + “:” + Bmac.hexdigest()
print ‘[DIFF]’ +Adir[Akey] + ” ” + Bdir[Akey]
del(Adir[Akey])
del(Bdir[Akey])
else:
print ‘[NEW]’+Adir[Akey]
del (Adir[Akey])
for keys in Bdir.keys():
print ‘[DEL]’+Bdir[keys]
print count
def main():
Adir = ‘/Users/Desktop/A/’
Bdir = ‘/Users/Desktop/B/’
Adic = get_list(Adir)
Bdic = get_list(Bdir)
get_diff(Adic, Bdic)
if __name__ == ‘__main__’:
main()