Skip to main content

Are these two files the same? Explicit file comparisons.

Project description

If you say “two files are the same”, you could mean at least three different things:

  • The files have the same os.stat() signature. (This is the shallow copy done by filecmp.cmp().)

  • The files have the same contents. They’re byte-for-byte identical.

  • They’re the same files on disk (modulo hard links).

You can compare files with filecmp or os, but it’s not always obvious what sort of comparison you’re doing.

Since explicit is better than implicit, filecmp2 provides three functions so you can be clear about what you mean by “same”:

def cmp_path_contents(path1, path2):
    """
    Returns True if the files at paths ``path1`` and ``path2``
    have the same contents.
    """


def cmp_stat(path1, path2):
    """
    Returns True if the os.stat() signature of ``path1`` and ``path2``
    are the same.
    """


def cmp_same_file(path1, path2):
    """
    Returns True if ``path1`` and ``path2`` point to the same file on disk.
    """

If you have two file-like objects, you can also compare their contents with cmp_contents:

>>> import filecmp2
>>> import io

>>> b1 = io.BytesIO(b"hello world")
>>> b2 = io.BytesIO(b"hello world")
>>> filecmp2.cmp_contents(b1, b2)
True

>>> b1 = io.BytesIO(b"hello world")
>>> b2 = io.BytesIO(b"the cheese shop")
>>> filecmp2.cmp_contents(b1, b2)
False

This is useful if you’re dealing with streams that you don’t want to write to disk.

I wrote this after discovering that I was using filecmp.cmp() wrong, and doing a shallow copy instead of checking the contents of the files. I don’t find the current API very clear, and reading the Python bug tracker suggests I’m not the only person who’s made this mistake. Although the docs explain the distinction, it’s lost on somebody who’s casually reading the code or reviewing without the docs.

Installation

Install from PyPI (pip install filecmp2), or copy the single file directly into your codebase.

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filecmp2-1.0.0-reupload.tar.gz (2.2 kB view details)

Uploaded Source

File details

Details for the file filecmp2-1.0.0-reupload.tar.gz.

File metadata

  • Download URL: filecmp2-1.0.0-reupload.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.2

File hashes

Hashes for filecmp2-1.0.0-reupload.tar.gz
Algorithm Hash digest
SHA256 343fb7cbed3429dcd681e763fae54f6ae51ba8c8ed51d5bf07b6e0ade22d2855
MD5 cf71e26f04d1c10341ed6c3d6f46a736
BLAKE2b-256 e14f769e92d8f7ab6f357e34cb9c6df76070ae699129c39dbe615243762285fa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page