{"id":1175,"date":"2023-07-24T18:34:00","date_gmt":"2023-07-24T13:04:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1175"},"modified":"2024-03-01T17:10:52","modified_gmt":"2024-03-01T11:40:52","slug":"filecmp-module-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/filecmp-module-in-python","title":{"rendered":"Comparing Files and Directories Using filecmp Module in Python"},"content":{"rendered":"\n<p>You&#8217;ve probably heard of the&nbsp;<code>filecmp<\/code>&nbsp;module, which provides functions for programmatically comparing files and directories.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-comparing-files\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-comparing-files\"><\/a>Comparing Files<\/h1>\n\n\n\n<p>The&nbsp;<code>filecmp<\/code>&nbsp;module includes a function called&nbsp;<code>cmp()<\/code>&nbsp;that compares two files and returns&nbsp;<code>True<\/code>&nbsp;if they are equal,&nbsp;<code>False<\/code>&nbsp;otherwise.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-syntax\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-syntax\"><\/a>Syntax<\/h3>\n\n\n\n<p><code>filecmp.cmp(f1, f2, shallow=True)<\/code><\/p>\n\n\n\n<p><strong>Parameters<\/strong>&nbsp;&#8211;<\/p>\n\n\n\n<p><code>f1<\/code>: First filename<\/p>\n\n\n\n<p><code>f2<\/code>: Second filename<\/p>\n\n\n\n<p><code>shallow<\/code>: If set to&nbsp;<code>True<\/code>&nbsp;and the information(<code>os.stat<\/code>&nbsp;signatures) of the file are identical, the files are considered equal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-comparing-files-using-cmp\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-comparing-files-using-cmp\"><\/a>Comparing Files Using cmp()<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import filecmp\n\ncompare = filecmp.cmp('test_file1.txt', 'test_file2.txt')\nprint(compare)\n\n----------\nTrue<\/pre><\/div>\n\n\n\n<p>Both files (<code>test_file1.txt<\/code>&nbsp;and&nbsp;<code>test_file2.txt<\/code>) have the same content, size, and permissions, that&#8217;s why the above code returned&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>Most information in both files would be similar if you used the&nbsp;<code>os.stat()<\/code>&nbsp;function to compare them.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">stat1 = os.stat('test_file1.txt')\nprint(\"Information: test_file1.txt\")\nprint(stat1)\n\nstat2 = os.stat('test_file2.txt')\nprint(\"Information: test_file2.txt\")\nprint(stat2)<\/pre><\/div>\n\n\n\n<p>Some&nbsp;<code>os.stat()<\/code>&nbsp;function attributes will be the same in both files.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Information: test_file1.txt\nos.stat_result(st_mode=33206, st_ino=6473924465395070, st_dev=3836766283, st_nlink=1, st_uid=0, st_gid=0, st_size=20, st_atime=1689869596, st_mtime=1689856217, st_ctime=1689856083)\n\nInformation: test_file2.txt\nos.stat_result(st_mode=33206, st_ino=2814749768156544, st_dev=3836766283, st_nlink=1, st_uid=0, st_gid=0, st_size=20, st_atime=1689869596, st_mtime=1689856277, st_ctime=1689856094)<\/pre><\/div>\n\n\n\n<p>The output shows that the status of both files is similar in terms of&nbsp;<code>st_mode<\/code>&nbsp;(permissions) and&nbsp;<code>st_size<\/code>&nbsp;(file size).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-comparing-files-having-different-info\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-comparing-files-having-different-info\"><\/a>Comparing Files Having Different Info<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import filecmp\n\nfile_path1 = 'test_file1.txt'\nfile_path2 = 'D:\/SACHIN\/Pycharm\/file_handling\/test.txt'\n\ncompare = filecmp.cmp(file_path1, file_path2, shallow=True)\nprint(compare)\n\n----------\nFalse<\/pre><\/div>\n\n\n\n<p>The above code returned&nbsp;<code>False<\/code>&nbsp;because the contents of both files differ, as does the file size.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-comparing-files-from-different-directories\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-comparing-files-from-different-directories\"><\/a>Comparing Files From Different Directories<\/h1>\n\n\n\n<p>Files from two different directories can be compared using the&nbsp;<code>filecmp.cmpfiles()<\/code>&nbsp;function.<\/p>\n\n\n\n<p>The function compares the common files in the directories specified and returns three results.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>match<\/code>: A list of filenames that are shared by both directories and have the same content.<\/li>\n\n\n\n<li><code>mismatch<\/code>: A list of filenames that are shared by both directories but contain different content.<\/li>\n\n\n\n<li><code>errors<\/code>: A list of filenames that were unable to be compared.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-syntax-1\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-syntax-1\"><\/a>Syntax<\/h3>\n\n\n\n<p><code>filecmp.cmpfiles(dir1, dir2, common, shallow=True)<\/code><\/p>\n\n\n\n<p><strong>Parameters<\/strong>&nbsp;&#8211;<\/p>\n\n\n\n<p><code>dir1<\/code>: First directory path<\/p>\n\n\n\n<p><code>dir2<\/code>: Second directory path<\/p>\n\n\n\n<p><code>common<\/code>: A list of filenames from&nbsp;<code>dir1<\/code>&nbsp;and&nbsp;<code>dir2<\/code><\/p>\n\n\n\n<p><code>shallow<\/code>: If set to&nbsp;<code>True<\/code>&nbsp;and the information(<code>os.stat<\/code>&nbsp;signatures) of the file are identical, the files are considered equal.<\/p>\n\n\n\n<p>For this section, consider the following directory structure with two directories called&nbsp;<code>first_dir<\/code>&nbsp;and&nbsp;<code>second_dir<\/code>&nbsp;and the following filenames:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"351\" height=\"260\" src=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3.jpeg\" alt=\"File structure in two different directories\" class=\"wp-image-1198\" srcset=\"https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3.jpeg 351w, https:\/\/geekpython.in\/wp-content\/uploads\/2023\/08\/image-3-300x222.jpeg 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-example\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-example\"><\/a>Example<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import filecmp\n\nfile_dir1 = 'first_dir'\nfile_dir2 = 'second_dir'\n\ncommon_files = ['basic.txt', 'demo.txt', 'sample.txt', 'test.txt']\n\nmatched, mismatch, not_compared = filecmp.cmpfiles(file_dir1, \n                                                   file_dir2, \n                                                   common=common_files)\nprint(f\"Matched: {matched}\")\nprint(f\"Unmatched: {mismatch}\")\nprint(f\"Unable to Compare: {not_compared}\")<\/pre><\/div>\n\n\n\n<p>The paths to both directories were specified in the above code, and the list of filenames to be compared was saved in the variable&nbsp;<code>common_files<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>filecmp.cmpfiles()<\/code>&nbsp;function was then called, and the directories and list of filenames were passed inside the function and assigned to three variables:&nbsp;<code>matched<\/code>,&nbsp;<code>mismatch<\/code>, and&nbsp;<code>not_compared<\/code>. The results were then printed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Matched: ['sample.txt', 'test.txt']\nUnmatched: ['demo.txt']\nUnable to Compare: ['basic.txt']<\/pre><\/div>\n\n\n\n<p>The filenames&nbsp;<code>sample.txt<\/code>&nbsp;and&nbsp;<code>test.txt<\/code>&nbsp;matched because they have the same content and are found in both directories. The&nbsp;<code>demo.txt<\/code>&nbsp;file does not match due to different content, and the&nbsp;<code>basic.txt<\/code>&nbsp;file cannot be compared because one of the directories lacks the&nbsp;<code>basic.txt<\/code>&nbsp;file to compare with.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-dircmp-perform-directory-comparisons-on-various-factors\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-dircmp-perform-directory-comparisons-on-various-factors\"><\/a>dircmp &#8211; Perform Directory Comparisons on Various Factors<\/h1>\n\n\n\n<p>The&nbsp;<code>filecmp.dircmp()<\/code>&nbsp;is used to create a&nbsp;<code>dircmp<\/code>&nbsp;object by passing the directories&#8217; paths to be compared. The&nbsp;<code>dircmp<\/code>&nbsp;class contains numerous methods and attributes that allow you to compare, analyze, differ, handle subdirectories, and much more by calling on the&nbsp;<code>dircmp<\/code>&nbsp;object.<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<p><code>filecmp.dircmp(a, b, ignore=None, hide=None)<\/code><\/p>\n\n\n\n<p><strong>Parameters<\/strong>&nbsp;&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a<\/code>: First directory path<\/li>\n\n\n\n<li><code>b<\/code>: Second directory path<\/li>\n\n\n\n<li><code>ignore<\/code>: Specifies the list of filenames to be ignored during comparison.<\/li>\n\n\n\n<li><code>hide<\/code>: Specifies the list of filenames to hide in the output.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-creating-a-dircmp-object\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-creating-a-dircmp-object\"><\/a>Creating a dircmp Object<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import filecmp\n\nfile_dir1 = 'first_dir'\nfile_dir2 = 'second_dir'\n\ndircmp_obj = filecmp.dircmp(file_dir1, file_dir2)\nprint(dircmp_obj)\n\n----------\n&lt;filecmp.dircmp object at 0x000001FE7ECF5A80&gt;<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>dircmp<\/code>&nbsp;object is created by invoking&nbsp;<code>filecmp.dircmp()<\/code>&nbsp;with the paths to the directories to be compared (<code>file_dir1<\/code>&nbsp;and&nbsp;<code>file_dir2<\/code>). By calling the methods and attributes on&nbsp;<code>dircmp_obj<\/code>, the directories can now be compared on various criteria.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-generating-comparison-report\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-generating-comparison-report\"><\/a>Generating Comparison Report<\/h3>\n\n\n\n<p>The&nbsp;<code>report()<\/code>&nbsp;method generates a report comparing the specified directories.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">dircmp_obj.report()\n\n----------\ndiff first_dir second_dir\nOnly in second_dir : ['basic.txt']\nIdentical files : ['sample.txt', 'test.txt']\nDiffering files : ['demo.txt']<\/pre><\/div>\n\n\n\n<p>Calling&nbsp;<code>report()<\/code>&nbsp;on&nbsp;<code>dircmp_obj<\/code>&nbsp;compared the two directories, revealing that&nbsp;<code>sample.txt<\/code>&nbsp;and&nbsp;<code>test.txt<\/code>&nbsp;files were identical, the&nbsp;<code>basic.txt<\/code>&nbsp;file was only found in the&nbsp;<code>second_dir<\/code>&nbsp;directory, and&nbsp;<code>demo.txt<\/code>&nbsp;files were found in both directories but their contents differ.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-identifying-missing-files\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-identifying-missing-files\"><\/a>Identifying Missing Files<\/h3>\n\n\n\n<p>The&nbsp;<code>left_only<\/code>&nbsp;and&nbsp;<code>right_only<\/code>&nbsp;attributes can be used to display filenames that are only found in the left (<code>a<\/code>) or right (<code>b<\/code>) directories. In simple words, you can find which file is present in one directory but missing in another directory.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Displaying filenames that are only present in left_dir\nfilenames_only_in_left_dir = dircmp_obj.left_only\nprint(f\"Filenames Only in Left Directory: {filenames_only_in_left_dir}\")\n\n# Displaying filenames that are only present in right_dir\nfilenames_only_in_right_dir = dircmp_obj.right_only\nprint(f\"Filenames Only in Right Directory: {filenames_only_in_right_dir}\")\n\n----------\nFilenames Only in Left Directory: []\nFilenames Only in Right Directory: ['basic.txt']<\/pre><\/div>\n\n\n\n<p>The output above shows that the&nbsp;<code>basic.txt<\/code>&nbsp;file is missing in the left directory (<code>first_dir<\/code>), but it exists in the right directory (<code>second_dir<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-listing-filenames\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-listing-filenames\"><\/a>Listing Filenames<\/h3>\n\n\n\n<p>The&nbsp;<code>left_list<\/code>&nbsp;and&nbsp;<code>right_list<\/code>&nbsp;can be used to list the filenames present in the left and right directories.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Listing filenames in left_dir\nfilenames_in_left_dir = dircmp_obj.left_list\nprint(f\"Filenames in Left Directory: {filenames_in_left_dir}\")\n\n# Listing filenames in right_dir\nfilenames_in_right_dir = dircmp_obj.right_list\nprint(f\"Filenames in Right Directory: {filenames_in_right_dir}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Filenames in Left Directory: ['demo.txt', 'sample.txt', 'test.txt']\nFilenames in Right Directory: ['basic.txt', 'demo.txt', 'sample.txt', 'test.txt']<\/pre><\/div>\n\n\n\n<p><strong>Similarly, the<\/strong>&nbsp;<code>left<\/code>&nbsp;<strong>and<\/strong>&nbsp;<code>right<\/code>&nbsp;<strong>attributes can be used to show the path of the left and right directories.<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">left_dir_path = dircmp_obj.left\nprint(f\"Path of Left Directory: {left_dir_path}\")\n\nright_dir_path = dircmp_obj.right\nprint(f\"Path of Right Directory: {right_dir_path}\")\n\n----------\nPath of Left Directory: first_dir\nPath of Right Directory: second_dir<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-analyzing-files\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-analyzing-files\"><\/a>Analyzing Files<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Displaying common files and subdirectories\ncommon_files_dir = dircmp_obj.common\nprint(f\"Common Files and Subdirectories: {common_files_dir}\")\n\n# Displaying common files\ncommon_files = dircmp_obj.common_files\nprint(f\"Common Files: {common_files}\")\n\n# Displaying common directories\ncommon_directories = dircmp_obj.common_dirs\nprint(f\"Common Directories: {common_directories}\")\n\n# Displaying same files\nsame_files = dircmp_obj.same_files\nprint(f\"Same Files: {same_files}\")\n\n# Displaying differ files\ndiffer_files = dircmp_obj.diff_files\nprint(f\"Unmatched Files: {differ_files}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Common Files and Subdirectories: ['demo.txt', 'sample.txt', 'test.txt']\nCommon Files: ['demo.txt', 'sample.txt', 'test.txt']\nCommon Directories: []\nSame Files: ['sample.txt', 'test.txt']\nUnmatched Files: ['demo.txt']<\/pre><\/div>\n\n\n\n<p>By examining the output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>common<\/code>&nbsp;returns a list of files and subdirectories that are shared by both directories.<\/li>\n\n\n\n<li><code>common_files<\/code>&nbsp;returns the list of files that are shared by both directories.<\/li>\n\n\n\n<li><code>common_dirs<\/code>&nbsp;returns a list of directories that are shared by both directories.<\/li>\n\n\n\n<li><code>same_files<\/code>&nbsp;returns a list of filenames that can be found in both directories and have the same content.<\/li>\n\n\n\n<li><code>diff_files<\/code>&nbsp;returns a list of filenames that exist in both directories but have different contents.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-ignoring-and-hiding-comparison-of-files\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-ignoring-and-hiding-comparison-of-files\"><\/a>Ignoring and Hiding Comparison of Files<\/h3>\n\n\n\n<p>If you wanted to ignore or hide any files from being compared, the&nbsp;<code>filecmp.dircmp<\/code>&nbsp;has parameters named&nbsp;<code>ignore<\/code>&nbsp;(a list of filenames to ignore) and&nbsp;<code>hide<\/code>&nbsp;(a list of filenames to hide).<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import filecmp\n\nfile_dir1 = 'first_dir'\nfile_dir2 = 'second_dir'\n\n# Filename to ignore\nignore = ['demo.txt']\n# Filename to hide\nhide = ['basic.txt']\n\n# Creating dircmp object\ndircmp_obj = filecmp.dircmp(file_dir1, file_dir2, ignore=ignore, hide=hide)\n\n# Generating comparison report\ndircmp_obj.report()\n\n# Listing the filenames in left directory\nfilenames_in_left_dir = dircmp_obj.left_list\nprint(f\"Filenames in Left Directory: {filenames_in_left_dir}\")\n\n# Listing the filenames in right directory\nfilenames_in_right_dir = dircmp_obj.right_list\nprint(f\"Filenames in Right Directory: {filenames_in_right_dir}\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">diff first_dir second_dir\nIdentical files : ['sample.txt', 'test.txt']\nFilenames in Left Directory: ['sample.txt', 'test.txt']\nFilenames in Right Directory: ['sample.txt', 'test.txt']<\/pre><\/div>\n\n\n\n<p>Both directories&#8217;&nbsp;<code>demo.txt<\/code>&nbsp;files were ignored, and the&nbsp;<code>basic.txt<\/code>&nbsp;file was hidden from comparison.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-clearing-cache\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-clearing-cache\"><\/a>Clearing Cache<\/h1>\n\n\n\n<p>The&nbsp;<code>filecmp<\/code>&nbsp;module includes a function called&nbsp;<code>clear_cache()<\/code>&nbsp;that allows you to clear the internal cache used by the&nbsp;<code>filecmp<\/code>&nbsp;module.<\/p>\n\n\n\n<p>When a file is modified and then compared in such a short period of time that the rounded-off modification time is nearly the same as the comparison time, the program may conclude that the files are identical.<\/p>\n\n\n\n<p>Sometimes certain situations may arise where you may get stuck while comparing files and getting odd results, in that case, you can give it a try to&nbsp;<code>filecmp.clear_cache()<\/code>&nbsp;function to clear any cache.<\/p>\n\n\n\n<p>Consider the following example, in which the cache is stored after comparing the two image files and then clearing the internal cache with the&nbsp;<code>filecmp.clear_cache()<\/code>&nbsp;function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import filecmp\n\nfile_dir1 = 'D:\/SACHIN\/Desktop\/rise.png'\nfile_dir2 = 'D:\/SACHIN\/Desktop\/media\/rise.png'\n\n# Comparing image file\ncompare = filecmp.cmp(file_dir1, file_dir2, shallow=False)\nprint(compare)\n# Printing the cache stored by filecmp\nprint(filecmp._cache)\n\n# Clearing cache\nfilecmp.clear_cache()\nprint(filecmp._cache)\n\n# Checking if cache is cleared or not\nassert len(filecmp._cache) == 0, 'Cache not cleared'<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code>assert<\/code>&nbsp;statement was written at the end of the code snippet to ensure that the cache is cleared (the module&#8217;s protected variable&nbsp;<code>_cache<\/code>&nbsp;is emptied properly), and if it is not, a message&nbsp;<code>'Cache not cleared'<\/code>&nbsp;is displayed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">True\n{('D:\/SACHIN\/Desktop\/rise.png', 'D:\/SACHIN\/Desktop\/media\/rise.png', (32768, 6516, 1689779926.7445374), (32768, 6516, 1689779926.7445374)): True}\n{}<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/filecmp-module-in-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>The&nbsp;<code>filecmp<\/code>&nbsp;module provides functions such as&nbsp;<code>cmp()<\/code>&nbsp;and&nbsp;<code>cmpfiles()<\/code>&nbsp;for comparing various types of files and directories, and the&nbsp;<code>dircmp<\/code>&nbsp;class provides numerous methods and attributes for comparing the files and directories on various factors.<\/p>\n\n\n\n<p>Let&#8217;s recall what you&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Comparing two different files<\/li>\n\n\n\n<li>Files from two different directories are being compared.<\/li>\n\n\n\n<li>The&nbsp;<code>dircmp<\/code>&nbsp;class and its methods and attributes are used to summarise, analyze, and generate reports on files and directories.<\/li>\n\n\n\n<li>Clearing the internal cache stored by the&nbsp;<code>filecmp<\/code>&nbsp;module using the&nbsp;<code>filecmp.clear_cache()<\/code>&nbsp;function.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/fileinput-module\" rel=\"noreferrer noopener\">How to read multiple files simultaneously using fileinput module in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/tempfile-in-python\" rel=\"noreferrer noopener\">Generate temporary files and directories using tempfile module in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-assert\" rel=\"noreferrer noopener\">assert statement &#8211; Debug your code using assert statements in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/asterisk-in-python\" rel=\"noreferrer noopener\">Understanding the different uses of asterisk(*) in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/seek-and-tell-in-python\" rel=\"noreferrer noopener\">What is the difference between seek() and tell() in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/match-case-in-python\" rel=\"noreferrer noopener\">How to use match-case statements for pattern matching in Python<\/a>?<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/init-vs-new\" rel=\"noreferrer noopener\">__init__ vs __new__ methods in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-pathlib-module\" rel=\"noreferrer noopener\">How to manipulate paths using the pathlib module in Python<\/a>?<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ve probably heard of the&nbsp;filecmp&nbsp;module, which provides functions for programmatically comparing files and directories. Comparing Files The&nbsp;filecmp&nbsp;module includes a function called&nbsp;cmp()&nbsp;that compares two files and returns&nbsp;True&nbsp;if they are equal,&nbsp;False&nbsp;otherwise. Syntax filecmp.cmp(f1, f2, shallow=True) Parameters&nbsp;&#8211; f1: First filename f2: Second filename shallow: If set to&nbsp;True&nbsp;and the information(os.stat&nbsp;signatures) of the file are identical, the files are considered [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1177,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,42],"tags":[44,12,31],"class_list":["post-1175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-modules","tag-files","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":6,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1647,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1175\/revisions\/1647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1177"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}