ปัญหา
คุณต้องการเปรียบเทียบไฟล์ใน Python
โซลูชัน
โมดูล filecmp ใน Python ใช้เปรียบเทียบไฟล์และไดเรกทอรี
ฟังก์ชันหลัก
cmp(file1, file2[, shallow])- เปรียบเทียบไฟล์สองไฟล์ คืนTrueถ้าเหมือนกันFalseถ้าไม่เหมือน Defaultshallow=Trueใช้ metadata (os.stat) เทียบ ถ้าshallow=Falseจะเทียบเนื้อหาจริงcmpfiles(dir1, dir2, common[, shallow])- เปรียบเทียบไฟล์ในลิสต์commonระหว่างสองไดเรกทอรี คืน tuple สามลิสต์:match,mismatch,errorsdircmp(dir1, dir2 [, ignore[, hide]])- สร้างอ็อบเจกต์เปรียบเทียบไดเรกทอรี มีเมธอดและแอตทริบิวต์มากมายสำหรับรายงาน
เตรียมข้อมูลทดสอบ
import os
def makefile(filename, text=None):
with open(filename, 'w') as f:
f.write(text or filename)
def makedirectory(directory_name):
if not os.path.exists(directory_name):
os.mkdir(directory_name)
# สร้างโครงสร้างไดเรกทอรีและไฟล์ทดสอบ
present_directory = os.getcwd()
os.chdir(directory_name)
os.mkdir('dir1')
os.mkdir('dir2')
os.mkdir('dir1/common_dir')
os.mkdir('dir2/common_dir')
os.mkdir('dir1/dir_only_in_dir1')
os.mkdir('dir2/dir_only_in_dir2')
makefile('dir1/file_only_in_dir1')
makefile('dir2/file_only_in_dir2')
makefile('dir1/common_file', 'Hello, Writing Same Content')
makefile('dir2/common_file', 'Hello, Writing Same Content')
makefile('dir1/not_the_same')
makefile('dir2/not_the_same')
makefile('dir1/file_in_dir1', 'This is a file in dir1')
os.mkdir('dir2/file_in_dir1') # ชื่อเดียวแต่นี้เป็น directory
os.chdir(present_directory)
1. ตัวอย่าง filecmp.cmp
import filecmp
print('*** Common File:', filecmp.cmp('example/dir1/common_file', 'example/dir2/common_file'), filecmp.cmp('example/dir1/common_file', 'example/dir2/common_file', shallow=False))
print('*** Different Files:', filecmp.cmp('example/dir1/not_the_same', 'example/dir2/not_the_same'), filecmp.cmp('example/dir1/not_the_same', 'example/dir2/not_the_same', shallow=False))
print('*** Identical Files:', filecmp.cmp('example/dir1/file_only_in_dir1', 'example/dir1/file_only_in_dir1'), filecmp.cmp('example/dir1/file_only_in_dir1', 'example/dir1/file_only_in_dir1', shallow=False))
ผลลัพธ์:
*** Common File: True True
*** Different Files: False False
*** Identical Files: True True
2. ตัวอย่าง cmpfiles
import filecmp
import os
dir1_contents = set(os.listdir('example/dir1'))
dir2_contents = set(os.listdir('example/dir2'))
common = list(dir1_contents & dir2_contents)
common_files = [f for f in common if os.path.isfile(os.path.join('example/dir1', f))]
print(f'*** Common files are: {common_files}')
match, mismatch, errors = filecmp.cmpfiles('example/dir1', 'example/dir2', common_files)
print(f'*** Matched files are: {match}')
print(f'*** Mismatch files are: {mismatch}')
print(f'*** Errors files are: {errors}')
ผลลัพธ์:
*** Common files are: ['file_in_dir1', 'not_the_same', 'common_file']
*** Matched files are: ['common_file']
*** Mismatch files are: ['file_in_dir1', 'not_the_same']
*** Errors files are: []
3. เปรียบเทียบไดเรกทอรีแบบละเอียด
import filecmp
dc = filecmp.dircmp('example/dir1', 'example/dir2')
print(dc.report())
print(dc.report_full_closure())
จะแสดงรายงานครบ: ไฟล์ที่เหมือน/ต่าง/มีเฉพาะด้านเดียว ไดเรกทอรีร่วม/แยก funny cases (เปรียบเทียบไม่ได้) และ recursive ทุก subdirectory