Compare Two Folder Structures and Find Differences in Python
Walks two directories using os.walk, builds sets of relative paths, and prints items that exist in only one folder.
Python code
59 linesimport os
def compare_folders(path1, path2):
"""
Compare the file/folder structure of two directories and print differences.
"""
def get_structure(root):
structure = set()
for dirpath, dirnames, filenames in os.walk(root):
rel_path = os.path.relpath(dirpath, root)
# Add directory marker
structure.add(f"DIR:{rel_path}")
# Add files
for f in filenames:
structure.add(os.path.join(rel_path, f))
return structure
struct1 = get_structure(path1)
struct2 = get_structure(path2)
only_in_first = struct1 - struct2
only_in_second = struct2 - struct1
if not only_in_first and not only_in_second:
print("Folders are identical in structure.")
else:
if only_in_first:
print(f"Only in {path1}:")
for item in sorted(only_in_first):
print(f" {item}")
if only_in_second:
print(f"Only in {path2}:")
for item in sorted(only_in_second):
print(f" {item}")
if __name__ == "__main__":
import tempfile
import os
# Create two temporary directories with slight differences
with tempfile.TemporaryDirectory() as dir1, tempfile.TemporaryDirectory() as dir2:
# Setup dir1
os.makedirs(os.path.join(dir1, "sub1"))
os.makedirs(os.path.join(dir1, "sub2"))
with open(os.path.join(dir1, "file1.txt"), "w") as f:
f.write("hello")
with open(os.path.join(dir1, "sub1", "file2.txt"), "w") as f:
f.write("world")
# Setup dir2 (identical except missing file2.txt and extra file)
os.makedirs(os.path.join(dir2, "sub1"))
os.makedirs(os.path.join(dir2, "sub2"))
with open(os.path.join(dir2, "file1.txt"), "w") as f:
f.write("hello")
with open(os.path.join(dir2, "extra.txt"), "w") as f:
f.write("extra")
print(f"Comparing folders:\n {dir1}\n {dir2}")
compare_folders(dir1, dir2)
Output
Comparing folders:
/tmp/tmpXXXXXX
/tmp/tmpYYYYYY
Only in /tmp/tmpXXXXXX:
sub1/file2.txt
Only in /tmp/tmpYYYYYY:
extra.txt
How it works
The get_structure helper recurses through all subdirectories with os.walk and records every file and directory relative to the root. Directory entries are prefixed with DIR: to distinguish them from files. Set difference (-) reveals items present in one folder but missing in the other. Sorting the output keeps the diff readable.
Common mistakes
- Forgetting to include directory markers — files and empty folders can be confused.
- Using absolute paths instead of relative ones, which breaks the diff logic.
- Assuming os.walk always visits folders in a consistent order.
Variations
- Use pathlib.Path.rglob with a set comprehension for a more modern style.
- Extend the comparison to also report file size or modification time differences.
Real-world use cases
- Verifying that a deployment script copied all files from a staging directory to production.
- Detecting unexpected files after running a build or code generation tool.
- Checking consistency between two backup snapshots of a project folder.
Sponsored
More from Files & data
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a Python Script That Detects and Deletes Empty Files Across Folders easy
- Compress and Extract ZIP Files Programmatically in Python easy
- Convert CSV Files to JSON in Python easy
- Convert Image to ASCII Art in Python medium
- Create a Personal Knowledge Base That Searches Notes Instantly in Python easy
Keep learning
Related tutorials and quizzes for this topic.