{"id":26848,"date":"2022-03-31T02:18:47","date_gmt":"2022-03-30T20:48:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=26848"},"modified":"2022-04-01T00:16:36","modified_gmt":"2022-03-31T18:46:36","slug":"python-program-to-compare-two-text-files","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-compare-two-text-files\/","title":{"rendered":"Python Program to Compare Two Text Files"},"content":{"rendered":"
Let us\u00a0look at how to compare two text files in Python in this tutorial. We will use a variety of approaches to try to solve this problem.<\/p>\n
Let us consider the below two text files as an example:<\/p>\n
demotextfile_1.txt:<\/strong><\/p>\n demotextfile_2.txt:<\/strong><\/p>\n Below are the ways to compare two text files in Python:<\/p>\n Python has a filecmp module that allows you to compare files. The filecmp.cmp() function returns a three-list of matched, errors, and mismatched files. filecmp.cmp() has two modes of operation.<\/p>\n shallow mode:<\/strong> In this mode, only file metadata such as size, modified date, and so on are compared. Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Let us\u00a0look at how to compare two text files in Python in this tutorial. We will use a variety of approaches to try to solve this problem. Let us consider the below two text files as an example: demotextfile_1.txt: hello this is Python-programs good morning all Then give a start to it welcome to the …<\/p>\nhello this is Python-programs \r\ngood morning all\r\nThen give a start to it \r\nwelcome to the greatest python coding platform<\/pre>\n
hello this is Python-programs \r\nAre you good at coding? \r\nThen give a start to it \r\ngood morning all<\/pre>\n
Program to Compare Two Text Files in Python<\/h2>\n
\n
Method #1: Using filecmp.cmp() Method<\/h3>\n
\ndeep mode:<\/strong> The content of the files is compared in this mode.<\/p>\n\n
# Import filecmp module using the import keyword \r\nimport filecmp\r\n# Import os module using the import keyword \r\nimport os\r\n# Give the first textfile as static input and store it in a variable.\r\nTextfile_1 = \"demotextfile_1.txt\"\r\n# Give the second textfile as static input and store it in another variable.\r\nTextfile_2 = \"demotextfile_2.txt\"\r\n\r\n# Pass the above given first and second text files as arguments to the cmp() function \r\n# of the filecmp module to compare metadata in shallow mode\r\n# Returns True if both the textfiles are same else False\r\nrslt = filecmp.cmp(Textfile_1, Textfile_2)\r\nprint(rslt)\r\n\r\n# Pass the above given first and second text files as arguments to the cmp() function \r\n# of the filecmp module to comapre content in deep mode( shallow=False)\r\n# Returns True if both the textfiles are same else False\r\nrslt = filecmp.cmp(Textfile_1, Textfile_2, shallow=False)\r\nprint(rslt)<\/pre>\n
False\r\nFalse<\/pre>\n
Method #2: Using line by line Comparision<\/h3>\n
\n
# Open the first text file in read-only mode using the open() function\r\n# by passing filename\/path, mode as arguments to it and store it in a variable.\r\nTextfile_1 = open(\"demotextfile_1.txt\", \"r\")\r\n# Open the second text file in read-only mode using the open() function\r\n# by passing filename\/path, mode as arguments to it and store it in another variable.\r\nTextfile_2 = open(\"demotextfile_2.txt\", \"r\")\r\n# Take a variable and initialize its value with 0 which represents the line number\r\ni = 0\r\n# Loop in each line of the given first text file using the for loop\r\nfor f1_line in Textfile_1:\r\n # Increment the above initialized\u00a0variable value by 1 which holds the line number\r\n i += 1\r\n # Loop in each line of the given second text file using the other nested for loop\r\n for f2_line in Textfile_2:\r\n # Check if each line of the first text file is equal\/similar to the corresponding line \r\n # of the second text file using the if conditional statement\r\n if f1_line == f2_line:\r\n # If it is true, then print the respective line number \r\n print(\"line_\",i,\": \")\r\n # Print Both the lines of given two text file are the same\r\n print(\"Both the lines of given two text file are the same\")\r\n print()\r\n else:\r\n # Else print the respective line number and corresponding lines of \r\n # both the text files\r\n print(\"line_\",i,\": \")\r\n print(\"Textfile_1: \", f1_line)\r\n print(\"Textfile_2: \", f2_line)\r\n break\r\n\r\n# Close the given first text file using the close() function\r\nTextfile_1.close()\r\n# Close the given second text file using the close() function\r\nTextfile_2.close()<\/pre>\n
line_ 1 : \r\nBoth the lines of given two text file are the same\r\n\r\nline_ 2 : \r\nTextfile_1: good morning all\r\n\r\nTextfile_2: Are you good at coding?\r\n\r\nline_ 3 : \r\nBoth the lines of given two text file are the same\r\n\r\nline_ 4 : \r\nTextfile_1: welcome to the greatest python coding platform\r\n\r\nTextfile_2: good morning all<\/pre>\n","protected":false},"excerpt":{"rendered":"