SoFunction
Updated on 2025-03-01

Implementation method of file comparison through Python module filecmp

filecmp defines two functions for convenient comparison of files with folders:

    (f1, f2[, shallow]):

Compare whether the contents of the two files match. Parameters f1, f2 specify the path to the file to be compared. The optional parameter shallow specifies whether the properties of the file itself need to be considered when comparing files (the file properties can be obtained through the function). If the file content matches, the function returns True, otherwise it returns False.

import filecmp 
s = ("", "") 
print(s) 

The operation results are as follows:

Return True if the two files are the same, and False if different

    (dir1, dir2, common[, shallow]):

Compare whether the specified files in the two folders are equal. Parameters dir1 and dir2 specify the folder to be compared, and parameter common specifies the list of file names to be compared. The function returns a tuple containing 3 list elements, representing a list of matches, mismatches and errors. An incorrect file refers to a file that does not exist, or the file is trivial and unreadable, or the file is not authorized to read, or the file cannot be accessed for other reasons.

import filecmp 
match,mismatch,errors = ("qin", "fu",['','']) 
print("%s\n%s\n%s"%(match,mismatch,errors)) 

The operation results are as follows:

[''] -- means that both files have the file and exactly match
[''] -- means that both files have the file, but do not match
[]           -- A file that indicates no errors

A dircmp class is defined in the filecmp module to compare folders. By comparing two folders, you can get some detailed comparison results (such as only a list of files that exist in folder A), and support recursive comparison of subfolders.

   dircmp provides three methods for reporting comparison results:

report(): only compare the contents in the specified folder (files and folders) report_partial_closure(): compare the contents of folders and first-level subfolders report_full_closure(): recursively compare the contents of all folders

Example: The file "" is contained in the folder "qin", the file "" and "" are contained in the folder "fu", and the content of the file "" under the two folders is the same.

<span style="font-size: 12px;">import filecmp 
x = ("qin","fu") 
diff = () 
print(diff)</span> 

The output result is as follows:

diff qin fu  
Only in fu: [""] 
Identical files: [""] 

If the content of the files "" under the two folders is different, the result is as follows:

The output result is as follows:

diff qin fu 
Only in fu: [""] 
Differing files: [""] 

dircmp also provides the following properties for obtaining detailed results for comparison:

•left_list: the list of files and folders in the left folder;
•right_list: the list of files and folders in the right folder;
•common: a file or folder that exists in both folders;
•left_only: Only files or folders that exist in the left folder;
•right_only: Only files or folders that exist in the right folder;
•common_dirs: subfolders that exist on both folders;
•common_files: subfiles that exist on both folders;
•common_funny: a subfolder that exists on both folders;
•same_files: matching file;
•diff_files: a mismatched file;
•funny_files: Files that exist in both folders but cannot be compared;
•subdirs: I didn't understand the meaning of this property. The explanation in the python manual is as follows: A dictionary mapping names in common_dirs to dircmp objects

Summarize

The above is the implementation method of file comparison through the Python module filecmp introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!