Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does a non-shallow filecmp.cmp do?

I'm using Python 2.6.2. The docs for the filecmp module say:

The filecmp module defines functions to compare files and directories, with various optional time/correctness trade-offs.

and, of the filecmp.cmp function:

filecmp.cmp(f1, f2[, shallow])

Compare the files named f1 and f2, returning True if they seem equal, False otherwise.

Unless shallow is given and is false, files with identical os.stat() signatures are taken to be equal.

What they don't do is specify just what is the correctness level one obtains with shallow=False. So, what does shallow=False do? How correct is it?

like image 424
vanden Avatar asked Aug 06 '10 06:08

vanden


People also ask

What does Filecmp CMP do?

filecmp. cmp() method in Python is used to compare two files. This method by default performs shallow comparison (as by default shallow = True ) that means only the os. stat() signatures (like size, date modified etc.)

How do I compare two directories in Python?

cmpfiles() method in Python is used to compare files in two directories. Multiple files can be compared using this method. This method returns three lists of file names namely match, mismatch and errors.

How do I compare a list of files in Python?

The filecmp module in python can be used to compare files and directories. 1. filecmp Compares the files file1 and file2 and returns True if identical, False if not. By default, files that have identical attributes as returned by os.

How do I check if a CMP file is shallow?

filecmp. cmp (f1, f2, shallow=True) ¶ Compare the files named f1 and f2, returning True if they seem equal, False otherwise. If shallow is true, files with identical os.stat () signatures are taken to be equal. Otherwise, the contents of the files are compared.

What is the use of the filecmp module?

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

How do I compare two files in CMP?

The filecmp module defines the following functions: filecmp. cmp (f1, f2, shallow=True) ¶ Compare the files named f1 and f2, returning True if they seem equal, False otherwise. If shallow is true, files with identical os.stat () signatures are taken to be equal.

How are files treated when Shallow is true?

If shallow is true and the os.stat () signatures (file type, size, and modification time) of both files are identical, the files are taken to be equal. Otherwise, the files are treated as different if their sizes or contents differ.


1 Answers

Consulting the source filecmp.py reveals that if shallow=False, filecmp.cmp first checks a few select properties of os.stat(), regardless of whether shallow is True or False. If the stat properties that are examined are the same, it returns True. Else, it checks its internal cache to see if the files have already been compared earlier. If it has, it returns True. Else, it reads BUFSIZE = 8*1024 chunks of data from both files and does an exact contents comparison until it reaches the end of the file. It returns True if the two files have exactly the same contents.

like image 194
vanden Avatar answered Sep 30 '22 08:09

vanden