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。
示例
以下是一个使用filecmp模块的示例−
import filecmp
import os
# 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)
输出
以下是上述代码的输出结果-
The two files are different.
使用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")
极客笔记