python3递归比较目录

2024-04-19 08:00:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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。在

怎么了?在


Tags: path代码in目录onlynewforos
2条回答
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)

听起来好像子目录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上递归所需的信息。检查它的^{}属性。您可以重构代码,以便只调用filecmp.dircmp一次,然后将结果传递到递归函数中以打印出来。在

相关问题 更多 >