The Python module learns about filecmp file comparison

  • 2020-04-02 09:36:17
  • OfStack

Filecmp defines two functions for easily comparing files with folders:

Filecmp. CMP (f1, f2, shallow []) :

Compare the contents of two files to see if they match. Parameters f1, f2 specify the path of the file to be compared. The optional shallow parameter specifies whether the attributes of the file itself need to be considered when comparing files (file attributes can be obtained through the os.stat function). The function returns True if the file contents match, or False otherwise.

Filecmp.cmpfiles (dir1, dir2, common[, shallow]) :

Compares the specified files in two folders for equality. Parameter dir1, dir2 specifies the folder to compare, and parameter common specifies the list of file names to compare. The function returns a tuple of three list elements representing a list of files that match, mismatch, and error. An error file is a file that does not exist, or is blocked as unreadable, or has no permission to read the file, or cannot be accessed for other reasons.

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

Dircmp provides three methods for reporting the results of comparisons:

The & # 8226; Report () : only compare the contents of the specified folder (files and folders)
The & # 8226; Report_partial_closure () : compare the contents of folders and first-level subfolders
The & # 8226; Report_full_closure () : recursively compares the contents of all folders
Dircmp also provides the following attributes to get the detailed results of the comparison:

The & # 8226; Left_list: list of files and folders in the left folder;
The & # 8226; Right_list: list of files and folders in the right folder;
The & # 8226; Common: a file or folder that exists in both folders;
The & # 8226; Left_only: a file or folder that exists only in the left folder;
The & # 8226; Right_only: a file or folder that exists only in the right folder;
The & # 8226; Common_dirs: a subfolder where both folders exist;
The & # 8226; Common_files: subfiles that exist in both folders;
The & # 8226; Common_funny: a subfolder where both folders exist;
The & # 8226; Same_files: matched files;
The & # 8226; Diff_files: mismatched files;
The & # 8226; Funny_files: files that exist in both folders but cannot be compared;
The & # 8226; Subdirs: I don't see what this property means, as explained in the python manual: A dictionary mapping names in common_dirs to dircmp objects

Simple is beautiful! I only want the result of the file comparison, don't want to care how the file is compared, hey, just use python ~~

Related articles: