Skip to content

Instantly share code, notes, and snippets.

@recursive-beast
Created May 26, 2020 04:37
Show Gist options
  • Save recursive-beast/bd40c9954d3302fd01d7583e25228098 to your computer and use it in GitHub Desktop.
Save recursive-beast/bd40c9954d3302fd01d7583e25228098 to your computer and use it in GitHub Desktop.
Compare two directories recursively
import filecmp
def is_same_dirs(*args, dircmp: filecmp.dircmp = None) -> bool:
"""
Compare two directories recursively.
Files in each directory are assumed to be equal if their names and contents are equal.
:param dircmp: a filecmp.dircmp object that will be
used as the directory comparison object.
:returns: True if the directory trees are the same
and there were no errors while accessing the directories or files,
False otherwise.
"""
if not dircmp:
dircmp = filecmp.dircmp(*args)
if dircmp.left_only or dircmp.right_only or dircmp.diff_files or dircmp.funny_files:
return False
for sub_dircmp in dircmp.subdirs.values():
if not is_same_dirs(dircmp=sub_dircmp):
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment