如何在Python中逐行比较两个不同的文件?
本教程介绍了使用Python可用模块、读取两个文件并逐行比较它们来执行这种典型工作的各种Python比较技术。
在Python中,可以通过多种方式比较两个文件。
逐行比较两个文本文件
通过使用open()函数从两个文本文件读取数据,我们可以比较它们包含的信息。open()函数将搜索本地目录并可能读取它。
示例
在此示例中,我们对比两个包含Python数据的文件。我们被告知这两个Python文件可能不相同,Python将为我们检查文件。可以使用readlines()方法从Python文件中提取行。
file1 = open('D:\Work TP/moving files.py', 'r')
file2 = open('D:\Work TP/mysql_access.py', 'r')
File3 = open('D:\Work TP/trial.py', 'w')
for line1 in file1:
for line2 in file2:
if line1 == line2:
File3.write("%s\n" %(line1))
File3.close()
file1.close()
file2.close()
在从文件中提取数据后,使用for循环逐行比较文件。如果行不匹配,用户将收到一条消息,告诉他们发生了不匹配的位置。为了让用户能够简单地找到不同的行,我们将包含数据本身。
输出
以下是上述代码的输出-
Line 1 doesn't match.
------------------------
File1: # importing the modules
File2: import mysql.connector
Line 2 doesn't match.
------------------------
File1: import os
File2: sampleDB=mysql.connector.connect(host='localhost',user='root',password='password',database='textx')
Line 3 doesn't match.
------------------------
File1: import shutil
File2: #print (sampleDB.connection_id)
Line 4 doesn't match.
------------------------
File1: # Providing the folder path
File2: cur=sampleDB.cursor()
Line 5 doesn't match.
------------------------
File1: origin = 'C:\Users\Lenovo\Downloads\Works\'
File2: s="CREATE TABLE novel(novelid integer(4),name varchar(20),price float(5,2))"
Line 6 doesn't match.
------------------------
File1: target = 'C:\Users\Lenovo\Downloads\Work TP\'
File2: cur.execute(s)
使用filecmp模块
通过filecmp模块可以在Python中处理文件。该模块专门设计用于比较两个或多个文件之间的数据。使用filecmp.cmp()方法,我们可以完成此操作。如果文件匹配,则该函数将返回True;否则,返回False。
示例
以下是使用filecmpule的示例−
import difflib
with open("D:\Work TP/moving files.py",'r') as file1:
file1_info = file1.readlines()
with open("D:\Work TP/trial.py",'r') as file2:
file2_info = file2.readlines()
diff = difflib.Differ().compare(file1_info, file2_info)
for line in diff:
print(line)
输出
- # importing the modules
? ^
+ import filecmp
import os
- # Providing the folder path
-
- origin = 'C:\Users\Lenovo\Downloads\Works\'
-
- target = 'C:\Users\Lenovo\Downloads\Work TP\'
- # Fetching the list of all the files
-
- files = os.listdir(origin)
- # Fetching all the files to directory
-
- for file_name in files:
- shutil.copy2(origin+file_name, target+file_name)
-
- print("Files are copied succesfully")
+ # notice the two backslashes
+ File1 = "D:\Work TP\moving files.py"
+ File2 = "D:\Work TP\trial.py"
+ def compare_files(File1,File2):
+ compare = filecmp.cmp(File1,File2)
+
+ if compare == True:
+ print("The two files are the same.")
+ else:
+ print("The two files are different.")
+
+ compare_files(File1,File2)
使用difflib模块
为了比较文本并识别它们之间的变化,请使用difflib包。Python 3模块已经预装了此语言。它具有许多有用的比较文本样本的功能。
首先,我们将使用unified diff()函数识别两个数据文件之间的差异。然后,我们将比较这些文件。
示例1-使用unified-diff()
在下面的例子中,使用with语句读取文件数据。Python with语句允许我们安全地打开和读取文件。
import difflib
with open("D:\Work TP/moving files.py",'r') as file1:
file1_info = file1.readlines()
with open("D:\Work TP/trial.py",'r') as file2:
file2_info = file2.readlines()
diff = difflib.unified_diff(
file1_info, file2_info, fromfile="file1.py",
tofile="file2.py", lineterm='')
for lines in diff:
print(lines)
输出
下面是上述代码的输出 –
--- file1.py
+++ file2.py
@@ -1,12 +0,0 @@
-# importing the modules
-import os
-import shutil
-# Providing the folder path
-origin = 'C:\Users\Lenovo\Downloads\Works\'
-target = 'C:\Users\Lenovo\Downloads\Work TP\'
-# Fetching the list of all the files
-files = os.listdir(origin)
-# Fetching all the files to directory
-for file_name in files:
- shutil.copy2(origin+file_name, target+file_name)
-print("Files are copied succesfully")
示例2-使用differ()
在difflib库中,有一个名为Differ的类可用于比较文件差异。该类用于比较文本行组并生成增量或差异。
以下是使用differ()方法逐行比较两个不同文件的示例 –
from difflib import Differ
with open('D:\Work TP/moving files.py') as file1, open('D:\Work TP/trial.py') as file2:
vary = Differ()
for lines in vary.compare(file1.readlines(), file2.readlines()):
print(lines)
输出
以下是上述代码的输出 –
- # importing the modules
- import os
- import shutil
- # Providing the folder path
- origin = 'C:\Users\Lenovo\Downloads\Works\'
- target = 'C:\Users\Lenovo\Downloads\Work TP\'
- # Fetching the list of all the files
- files = os.listdir(origin)
- # Fetching all the files to directory
- for file_name in files:
- shutil.copy2(origin+file_name, target+file_name)
- print("Files are copied succesfully")
极客教程