Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

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.

Easy Python 3.9+ Jun 27, 2026 Files & data 1 views 0 copies

Python code

59 lines
Python 3.9+
import 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

stdout
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

  1. Use pathlib.Path.rglob with a set comprehension for a more modern style.
  2. 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

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.