字符串 文件 目录对比

两个字符串对比
difflib库
例子
import difflib

text1 = '''text1:
Williams was talked out of retirement last year by his wife following a slump in form that was \
so profound that he didn't even qualify for the Crucible.
difflib document v7.4
add string'''
text1_lines = text1.splitlines() #以行分割
text2 = '''text2:
Williams was talked out of retirement last year by his wife following a slump in form that was \
so profound that he didn't even qualify for the Crucible.
difflib document v7.5'''

text2_lines = text2.splitlines()

d = difflib.Differ() #Differ函数比较字符串
diff = d.compare(text1_lines,text2_lines)
print('\n'.join(list(diff)))

#结果为

- text1:

? ^ ?号表示两个序列有增量差异,^号表示差异字符,中间的空表示两个序列一致

+ text2:

?



d = difflib.Differ() #Differ函数比较字符串
diff = d.compare(text1_lines,text2_lines)
print('\n'.join(list(diff)))
替换为下面的代码
d = difflib.HtmlDiff() #htmldiff函数将结果保存为html格式
print(d.make_file(text1_lines,text2_lines))

两个文件对比(cmp)
例子
import filecmp,sys,os
BASE_DIR = os.path.dirname(os.path.abspath(file))
d = filecmp.cmp(BASE_DIR+'\'+'nginx.conf',BASE_DIR+'\'+'nginx2.conf',shallow=False) #默认shallow为True,不比较文件内容,只比较文件基本信息(stat命令可以查看文件的基本信息)
print(d)

目录对比(dircmp)
例子
import filecmp,sys,os
BASE_DIR = os.path.dirname(os.path.abspath(file))
dir1 = '%s/%s'%(BASE_DIR,'db1')
dir2 = '%s/%s'%(BASE_DIR,'db2')

res = filecmp.dircmp(dir1,dir2,['nginx.conf']) #方括号中的表示忽略
res.report() #比较指定目录
res.report_partial_closure()#比较指定目录以及第一级目录
res.report_full_closure()#递归比较所有指定目录

print('left_list:'+str(res.left_list)) #左目录列表
print('right_list:'+str(res.right_list))#右目录列表
print('common:'+str(res.common))#两个目录共同的
print('left_only:'+str(res.left_only))#仅在左目录
print('right_only:'+str(res.right_only))#仅在右目录
print('common_dirs:'+str(res.common_dirs))#两边都有的子目录
print('common_files:'+str(res.common_files))#两边都有的子文件
print('common_funny:'+str(res.common_funny))
print('same_files:'+str(res.same_files)) #匹配相同的
print('diff_files:'+str(res.diff_files)) #不匹配的
print('funny_files:'+str(res.funny_files)) #两边都有不能比较的

例子
import os, sys
import filecmp
import re
import shutil

holderlist = []

def compareme(db1, db2):
'''比较两个目录,将只有左边目录有的文件和不匹配的文件放到列表'''
dircomp = filecmp.dircmp(db1, db2)
only_in_one = dircomp.left_only #获取只有db1中存在的文件
diff_in_one = dircomp.diff_files #获取不匹配的文件
dirpath = os.path.abspath(db1)
[holderlist.append(os.path.abspath(os.path.join(db1, x))) for x in only_in_one]
[holderlist.append(os.path.abspath(os.path.join(db1, x))) for x in diff_in_one]
if len(dircomp.common_dirs) > 0:
for item in dircomp.common_dirs: #遍历相同子目录的文件
compareme(os.path.abspath(os.path.join(db1, item)), \
os.path.abspath(os.path.join(db2, item)))
return holderlist

def main():
if len(sys.argv) > 2:#获取输入的两个目录
db1 = sys.argv[1]
db2 = sys.argv[2]
else:
print("Usage: ", sys.argv[0], "datadir backupdir")
sys.exit()

source_files = compareme(db1, db2)
db1 = os.path.abspath(db1)

if not db2.endswith('/'): db2 = db2 + '/'
db2 = os.path.abspath(db2)
destination_files = []

for item in source_files:#统计db2中和db1中不一致的文件
    destination_dir = re.sub(db1, db2, item)
    print('destination_dir', destination_dir)
    destination_files.append(destination_dir)

print("update item:")
print(source_files)

copy_pair = zip(source_files, destination_files) #将匹配文件和不一致的文件的压成数组
for item in copy_pair:
    if os.path.isfile(item[0]):
        shutil.copyfile(item[0], item[1]) #copy不一致的文件到db2中

if name == 'main':
main()

================================
两个配置文件对比html格式例子
import difflib
import sys

try:
textfile1=sys.argv[1]
textfile2=sys.argv[2]
except Exception as e:
print("Error:"+str(e))
print("Usage: simple3.py filename1 filename2")
sys.exit()

def readfile(filename):
try:
fileHandle = open (filename, 'r' )
text=fileHandle.readlines()

text = fileHandle.read().splitlines()

    fileHandle.close()
    return text
except IOError as error:
   print('Read file Error:'+str(error))
   sys.exit()

text1_lines = readfile(textfile1)
text2_lines = readfile(textfile2)

d = difflib.HtmlDiff()
print(d.make_file(text1_lines,text2_lines))

转载于:https://blog.51cto.com/xiaoshanzi/2119092

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值