Content · Programming · Python

Python filecmp.dircmp Class: Usage and Examples

Compare directory contents with filecmp.dircmp(). filecmp.dircmp(left, right) reports left_only, right_only, common_files, and subdirectories for two directory trees.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This Python tutorial explains filecmp.dircmp Class: Usage and Examples. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.

What This Feature Does


Compare directory contents with filecmp.dircmp().

filecmp.dircmp(left, right) reports left_only, right_only, common_files, and subdirectories for two directory trees. Its file comparison uses shallow rules; drill into filecmp methods for detailed byte checks.

When to Use It


This technique belongs to file copying, temporary files and matching. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.

Step-by-Step Python Example


Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.

import filecmp
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temporary_folder:
    root_folder = Path(temporary_folder)
    first_folder = root_folder / "first"
    second_folder = root_folder / "second"
    first_folder.mkdir()
    second_folder.mkdir()
    (first_folder / "unique.txt").touch()
    difference = filecmp.dircmp(first_folder, second_folder)
    print(difference.left_only)

Understanding the Code


  • First, the imports make filecmp, pathlib, tempfile available. They provide the filesystem, data or standard-library functions used later.
  • Find the call TemporaryDirectory(). Its parameters determine the specific operation demonstrated in the title.
  • Its purpose is to compare directory contents with filecmp.dircmp(). Compare this description against the result printed by the program.
  • The sample uses TemporaryDirectory() so any practice files are removed after the context ends; it does not work on your real documents.
  • The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.

Expected Output


['unique.txt']

The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.

Important Details and Edge Cases


Use subdirs or phase4() when you need nested directory comparison.

filecmp.dircmp() reports directory differences; it does not automatically synchronize or copy files.

Practice: Try It Yourself


Compare two test folders with one common and one unique filename.

Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.

Official Python Documentation


For the complete reference, see the Python standard library documentation.

Common Beginner Mistakes


  • Expecting dircmp to automatically report complete deep differences for every nested file.
  • Misinterpreting left_only as files present in both directories.
  • filecmp.dircmp() reports directory differences; it does not automatically synchronize or copy files.

Key Takeaways


  • Compare directory contents with filecmp.dircmp().
  • filecmp.dircmp(left, right) reports left_only, right_only, common_files, and subdirectories for two directory trees. Its file comparison uses shallow rules; drill into filecmp methods for detailed byte checks.
  • Avoid this common pitfall: Expecting dircmp to automatically report complete deep differences for every nested file.
Share this resourceSend this page to someone who may find it useful.