`
忧里修斯
  • 浏览: 426426 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

文件夹差异文件对比工具

阅读更多
# -*- coding:UTF-8 -*-
'''
Created on 2010-4-30

@author: shiyong
'''
import os

class Compare(object):
    '''
            文件夹比较工具
            功能:目前只未比较文件的名称和大小,未比较md5
    '''

    def __init__(self):
        '''
        Constructor
        '''
    
    #遍历比较  
    def visit(self,newpath,oldpath):
        
        if os.path.isdir(newpath) and os.path.isdir(oldpath):
            
#            print '比较'+newpath+'和'+oldpath
            newdirs = os.listdir(newpath)#新文件夹列表
            olddirs = os.listdir(oldpath)#原文件夹列表
            for filef in newdirs:
                
                nfpath = newpath+'\\'+filef#新文件中子文件或子文件夹得绝对路径
                ofpath = oldpath+'\\'+filef#原文件中子文件或子文件夹得绝对路径
                if filef not in olddirs:#在原文件夹中不存在同名的
                    print '不存在'+ofpath
                else:#存在同名
                    #比较的都是文件
                    if os.path.isfile(nfpath) and os.path.isfile(ofpath):
                        if os.path.getsize(nfpath) != os.path.getsize(ofpath):
                            print '不存在'+ofpath+'原因:文件大小不一致'
                    
                    #比较的都是文件夹
                    elif os.path.isdir(nfpath) and os.path.isdir(ofpath):
                        self.visit(nfpath, ofpath)
                        
                    else:
                        pass
            
        else:
            print '请检查要比较的文件夹是否存在'
            return
        
if __name__ == '__main__':
      
    c = Compare()
    c.visit('D:\i-dong','D:\Ubuntu')   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics