python-clinic / Doc / library / filecmp.rst

Full commit

:mod:`filecmp` --- File and Directory Comparisons

Source code: :source:`Lib/`

The :mod:`filecmp` module defines functions to compare files and directories, with various optional time/correctness trade-offs. For comparing files, see also the :mod:`difflib` module.

The :mod:`filecmp` module defines the following functions:

The :class:`dircmp` class

Construct a new directory comparison object, to compare the directories a and b. ignore is a list of names to ignore, and defaults to :attr:`filecmp.DEFAULT_IGNORES`. hide is a list of names to hide, and defaults to [os.curdir, os.pardir].

The :class:`dircmp` class compares files by doing shallow comparisons as described for :func:`filecmp.cmp`.

The :class:`dircmp` class provides the following methods:

The :class:`dircmp` class offers a number of interesting attributes that may be used to get various bits of information about the directory trees being compared.

Note that via :meth:`__getattr__` hooks, all attributes are computed lazily, so there is no speed penalty if only those attributes which are lightweight to compute are used.

Here is a simplified example of using the subdirs attribute to search recursively through two directories to show common different files:

>>> from filecmp import dircmp
>>> def print_diff_files(dcmp):
...     for name in dcmp.diff_files:
...         print("diff_file %s found in %s and %s" % (name, dcmp.left,
...               dcmp.right))
...     for sub_dcmp in dcmp.subdirs.values():
...         print_diff_files(sub_dcmp)
>>> dcmp = dircmp('dir1', 'dir2') # doctest: +SKIP
>>> print_diff_files(dcmp) # doctest: +SKIP