File and directory difference comparison module filecmp

   Introduction

      When we perform code auditing or verify backup results, we often need to check the file consistency between the original and the target directory. The python standard library already comes with a module filecmp that meets this requirement. filecmp can realize the difference comparison function of files, directories, and traversing subdirectories. For example, the output target directory in the report is more than the original file or subdirectory, even if the file has the same name, it will judge whether it is the same file (content-level comparison), etc. Python 2.3 and above comes with the filecmp module, no additional installation is required.

Description of common methods of modules

       filecmp provides three operation methods, namely cmp (single file comparison), cmpfiles (multi-file comparison), dircmp (directory comparison)

For single-file comparison, the filecmp.cmp(f1,f2[,shallow]) method is used to compare the files with file names f1 and f2. The same returns True, and the different returns False. Shallow defaults to True, which means that only according to os.stat The basic information of the file returned by the () method is compared, such as access time, modification time, state change time, etc., and the comparison of file contents will be ignored. When shallow is False, os.stat() will check the contents of the file at the same time.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325014938&siteId=291194637