Python module learning-filecmp file comparison

Source: Internet
Author: User

The filecmp module is used to compare the content of files and folders. It is a lightweight tool and is easy to use. The Python standard library also provides the difflib module for comparing file content. About the difflib module, and listen to the next decomposition.

Filecmp defines two functions for convenient comparison of files and folders:

Filecmp. CMP (F1, F2 [, shallow]):

Compare the content of the two files. Parameter F1 and F2 specify the path of the file to be compared. The optional parameter shallow specifies whether to consider the attributes of the file when comparing files (the file attributes can be obtained through the OS. Stat function ). If the file content matches, the function returns true; otherwise, false.

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

Compare two foldersSpecified fileEqual or not. The dir1 and dir2 Parameters specify the folder to be compared, and the common parameter specifies the list of file names to be compared. The function returns the tuples containing three list elements, indicating the list of matched, mismatched, and incorrect files. A wrong file refers to a non-existent file, or the file is set to unreadable, or has no permission to read the file, or cannot be accessed for other reasons.

The filecmp module defines a dircmp class for comparing folders. By comparing two folders in this class, you can obtain detailed comparison results (such as the list of files only in folder ), and supports recursive comparison of subfolders.

Dircmp provides three methodsReport comparison results:

    • Report (): only compare the content in the specified folder (files and folders)
    • Report_partial_closure (): Compares the contents of the folder and the first-level subfolders.
    • Report_full_closure (): recursively compares the content of all folders.

Dircmp also provides the following attributesObtain detailed comparison results:

    • Left_list: list of files and folders in the left folder;
    • Right_list: list of files and folders in the right folder;
    • Common: files or folders in both folders;
    • Left_only: Only files or folders in the left folder;
    • Right_only: Only files or folders in the folder on the right;
    • Common_dirs: subfolders in both folders;
    • Common_files: subfiles in both folders;
    • Common_funny: subfolders in both folders;
    • Same_files: matched files;
    • Diff_files: unmatched files;
    • Funny_files: files that exist in both folders but cannot be compared;
    • Subdirs: I didn't understand the meaning of this attribute. The explanation in the python manual is as follows: a dictionary mapping names in common_dirs to dircmp objects

Simple is beautiful! I only need to compare the results of files and don't want to worry about how files are compared. Hey, use python ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.