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

后端 未结 11 1310
猫巷女王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:35

    The report_full_closure() method is recursive:

    comparison = filecmp.dircmp('/directory1', '/directory2')
    comparison.report_full_closure()
    

    Edit: After the OP's edit, I would say that it's best to just use the other functions in filecmp. I think os.walk is unnecessary; better to simply recurse through the lists produced by common_dirs, etc., although in some cases (large directory trees) this might risk a Max Recursion Depth error if implemented poorly.

提交回复
热议问题