Recursively compare two directories to ensure they have the same files and subdirectories

后端 未结 11 1273
猫巷女王i
猫巷女王i 2020-12-23 20:49

From what I observe filecmp.dircmp is recursive, but inadequate for my needs, at least in py2. I want to compare two directories and all their contained files. Do

11条回答
  •  一向
    一向 (楼主)
    2020-12-23 21:46

    Here is my solution: gist

    def dirs_same_enough(dir1,dir2,report=False):
        ''' use os.walk and filecmp.cmpfiles to
        determine if two dirs are 'same enough'.
    
        Args:
            dir1, dir2:  two directory paths
            report:  if True, print the filecmp.dircmp(dir1,dir2).report_full_closure()
                     before returning
    
        Returns:
            bool
    
        '''
        # os walk:  root, list(dirs), list(files)
        # those lists won't have consistent ordering,
        # os.walk also has no guaranteed ordering, so have to sort.
        walk1 = sorted(list(os.walk(dir1)))
        walk2 = sorted(list(os.walk(dir2)))
    
        def report_and_exit(report,bool_):
            if report:
                filecmp.dircmp(dir1,dir2).report_full_closure()
                return bool_
            else:
                return bool_
    
        if len(walk1) != len(walk2):
            return false_or_report(report)
    
        for (p1,d1,fl1),(p2,d2,fl2) in zip(walk1,walk2):
            d1,fl1, d2, fl2 = set(d1),set(fl1),set(d2),set(fl2)
            if d1 != d2 or fl1 != fl2:
                return report_and_exit(report,False)
            for f in fl1:
                same,diff,weird = filecmp.cmpfiles(p1,p2,fl1,shallow=False)
                if diff or weird:
                    return report_and_exit(report,False)
    
        return report_and_exit(report,True)
    

提交回复
热议问题