Python 3的递归比较目录

时间:2015-03-22 20:36:35

标签: python python-3.x

我需要比较目录。我使用以下代码(示例):

def dir_comp(dir1, dir2):
    filecmp.clear_cache()
    DirComp = filecmp.dircmp(dir1, dir2)
    if len(DirComp.left_only) > 0:
        for item in DirComp.left_only:
            print('File/folder', os.path.join(dir1, item), 'exist only in', dir1)
    if len(DirComp.right_only) > 0:
        for item in DirComp.right_only:
            print('File/folder', os.path.join(dir2, item), 'exist only in', dir2)
    for CommonDirs in DirComp.common_dirs:
        new_dir1 = os.path.join(dir1, CommonDirs)
        new_dir2 = os.path.join(dir2, CommonDirs)
        dir_comp(new_dir1, new_dir2)

但代码只显示根目录的不同文件:http://i.stack.imgur.com/zUf2i.png。查看目录之间真实不同的屏幕截图: http://i.stack.imgur.com/FpQTe.png

有什么问题?

2 个答案:

答案 0 :(得分:0)

听起来子目录n5/N51n51/N5只出现在一个地方(没有n5/N5目录,也没有n51/N51)。 dircmp的比较不会递归到那些目录,因为它提前知道每个文件都是不匹配的。

如果这不是您想要的,您应该使用os.walk检查其内容,以区别对待不匹配的常规文件。尝试这样的事情:

def dir_comp(dir1, dir2):
    filecmp.clear_cache()
    DirComp = filecmp.dircmp(dir1, dir2)
    for item in DirComp.left_only:
        path = os.path.join(dir1, item)
        if os.path.isdir(path):
            for base, subdirs, files in os.walk(path):
                print("Folder", base, "exists only in", dir1)
                for file in files:
                    print("File", os.path.join(base, file), "exists only in", dir1)
        else:
            print('File', path, 'exist only in', dir1)

    for item in DirComp.right_only:
        path = os.path.join(dir2, item)
        if os.path.isdir(path):
            for base, subdirs, files in os.walk(path):
                print("Folder", base, "exists only in", dir2)
                for file in files:
                    print("File", os.path.join(base, file), "exists only in", dir2)
        else:
            print('File', os.path.join(path), 'exist only in', dir2)

    for CommonDirs in DirComp.common_dirs:
        new_dir1 = os.path.join(dir1, CommonDirs)
        new_dir2 = os.path.join(dir2, CommonDirs)
        dir_comp(new_dir1, new_dir2)

这应该打印仅存在于一个文件夹中的所有文件(即使它们位于仅存在于该文件夹中的目录中)。请注意,您获得的DirComp对象已经拥有CommonDirs递归所需的信息。查看其subdirs属性。您可以重构代码,以便只调用filecmp.dircmp一次,然后将结果传递给递归函数以将其打印出来。

答案 1 :(得分:0)

def print_diff_files(dirs):
    for name in dirs.diff_files:
        diff_files.append(os.path.join(dirs.left, name))
        diff_files.append(os.path.join(dirs.right, name))
    for file_left in dirs.left_only:
        only_left.append(os.path.join(dirs.left, file_left))
    for file_right in dirs.right_only:
        only_right.append(os.path.join(dirs.right, file_right))
    for same_files in dirs.same_files:
        same_files_list.append(os.path.join(dirs.left, same_files))
        same_files_list.append(os.path.join(dirs.right, same_files))
    for sub_dirs in dirs.subdirs.values():
        print_diff_files(sub_dirs)

DirCompare = filecmp.dircmp('old', 'new')
print_diff_files(DirCompare)