递归比较两个目录以确保它们具有相同的文件和子目录 [英] Recursively compare two directories to ensure they have the same files and subdirectories

查看:42
本文介绍了递归比较两个目录以确保它们具有相同的文件和子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我观察到的filecmp.dircmp递归的,但不足以满足我的需要,至少在 py2 中是这样.我想比较两个目录及其包含的所有文件.这是否存在,或者我需要构建(使用 os.walk,例如).我更喜欢预先构建的,其他人已经完成了单元测试:)

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. Does this exist, or do I need to build (using os.walk, for example). I prefer pre-built, where someone else has already done the unit-testing :)

如果有帮助的话,实际的比较"可能很草率(例如,忽略权限).

The actual 'comparison' can be sloppy (ignore permissions, for example), if that helps.

我想要一些布尔值,report_full_closure 是一份打印报告.它也只会进入常见的子目录.AFIAC,如果他们在左侧或右侧目录中有任何内容only,那么它们是不同的目录.我使用 os.walk 来构建它.

I would like something boolean, and report_full_closure is a printed report. It also only goes down common subdirs. AFIAC, if they have anything in the left or right dir only those are different dirs. I build this using os.walk instead.

推荐答案

这里是带有 filecmp 模块的比较函数的替代实现.它使用递归而不是 os.walk,所以它更简单一些.但是,它不会简单地通过使用 common_dirssubdirs 属性进行递归,因为在这种情况下,我们将隐式使用文件比较的默认浅"实现,这可能不是你想要什么.在下面的实现中,当比较同名文件时,我们总是只比较它们的内容.

Here's an alternative implementation of the comparison function with filecmp module. It uses a recursion instead of os.walk, so it is a little simpler. However, it does not recurse simply by using common_dirs and subdirs attributes since in that case we would be implicitly using the default "shallow" implementation of files comparison, which is probably not what you want. In the implementation below, when comparing files with the same name, we're always comparing only their contents.

import filecmp
import os.path

def are_dir_trees_equal(dir1, dir2):
    """
    Compare two directories recursively. Files in each directory are
    assumed to be equal if their names and contents are equal.

    @param dir1: First directory path
    @param dir2: Second directory path

    @return: True if the directory trees are the same and 
        there were no errors while accessing the directories or files, 
        False otherwise.
   """

    dirs_cmp = filecmp.dircmp(dir1, dir2)
    if len(dirs_cmp.left_only)>0 or len(dirs_cmp.right_only)>0 or \
        len(dirs_cmp.funny_files)>0:
        return False
    (_, mismatch, errors) =  filecmp.cmpfiles(
        dir1, dir2, dirs_cmp.common_files, shallow=False)
    if len(mismatch)>0 or len(errors)>0:
        return False
    for common_dir in dirs_cmp.common_dirs:
        new_dir1 = os.path.join(dir1, common_dir)
        new_dir2 = os.path.join(dir2, common_dir)
        if not are_dir_trees_equal(new_dir1, new_dir2):
            return False
    return True

这篇关于递归比较两个目录以确保它们具有相同的文件和子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆