# diff_files.py
import sys
import difflib
# 从命令行参数中获取要比较的文件路径
try:
file1 = sys.argv[1]
file2 = sys.argv[2]
except Exception as err:
print("错误:" + str(err))
print("使用方法:python diff_files.py 文件1 文件2")
sys.exit()
''' 读取整个文件,自动生成逐行分隔的字符串列表 '''
def readfile(filename):
try:
with open(filename, 'r') as f:
return f.readlines()
except IOError as err:
print('读取文件错误:' + str(err))
sys.exit()
''' 比较两个文件,生成HTML格式的文档 '''
def compare_file(file1, file2):
file1_content= readfile(file1)
file2_content = readfile(file2)
# 实例化HtmlDiff()类以生成HtmlDiff对象
d = difflib.HtmlDiff()
# 通过make file()方法生成HTML格式的比较结果
result = d.make_file(file1_content, file2_content)
return result
if __name__ == '__main__':
result = compare_file(file1,file2)
# 将比较结果写入到结果文件中
with open("diff_result.html", 'w') as f:
f.writelines(result)
# example1.json
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://i4gs4xxq.mirror.aliyuncs.com"
],
"insecure-registries": [
"172.18.18.34:5000"
],
"max-concurrent-downloads": 10,
"log-opts": {
"cache-disabled": false,
"cache-compress": true,
"max-file": 5,
"max-size": "10m"
}
}
# example2.json
{
"registry-mirrors": [
"https://registry.docker-cn.com",
"https://i4gs4xxq.mirror.aliyuncs.com"
],
"insecure-registries": [
"172.18.18.34:5000",
"192.168.10.12:5000"
],
"max-concurrent-downloads": 15,
"log-opts": {
"cache-disabled": false,
"cache-compress": true,
"max-file": 5,
"max-size": "10m",
"cache-max-file": 5,
"cache-max-size": "20m"
}
}
diff_result.html
# cmp_dirs.py
import sys
import os
from filecmp import dircmp
# 定义全局变量便于递归处理
diff_files = [] # 名称相同但不一致的文件(目录)列表
left_only = [] # 第一个目录独有的文件(目录)列表
right_only = [] # 第二个目录独有的文件(目录)列表
# 从命令行参数中获取要比较的目录路径
try:
dir1 = sys.argv[1]
dir2 = sys.argv[2]
except Exception as err:
print("错误:" + str(err))
print("使用方法:python cmp_dirs.py 目录1 目录2")
sys.exit()
''' 处理目录比较结果,分类添加到列表中 '''
def cmp_result(dcmp):
for item in dcmp.diff_files:
diff_files.append(os.path.join(dcmp.left, item))
for item in dcmp.left_only:
left_only.append(os.path.join(dcmp.left, item))
for item in dcmp.right_only:
right_only.append(os.path.join(dcmp.right, item))
for sub_dcmp in dcmp.subdirs.values():
cmp_result(sub_dcmp) # 递归处理子目录
if __name__ == '__main__':
dcmp = dircmp(dir1, dir2) # 实例化dircmp类
print(dcmp.subdirs.values())
cmp_result(dcmp)
diff_count = len(diff_files) # 计算列表长度以获取文件(目录)数
left_count = len(left_only)
right_count = len(right_only)
if (diff_count == 0 and left_count == 0 and right_count == 0):
print("两个目录完全一致!")
else:
print("目录比较结果分析:")
print(dir1 + "目录中独有" + str(left_count) + "个文件: ", left_only)
print(dir2 + "目录中独有" + str(right_count) + "个文件: ", right_only)
print("名称相同但不一致的有" + str(diff_count) + "个文件: ", diff_files)
test@test:~/test/config$ python3 cmp_dirs.py /tmp ~/test
dict_values([])
目录比较结果分析:
/tmp目录中独有13个文件: ['/tmp/.ICE-unix', '/tmp/.Test-unix', '/tmp/.X11-unix', '/tmp/.XIM-unix', '/tmp/.font-unix', '/tmp/full-list', '/tmp/snap-private-tmp', '/tmp/systemd-private-f2bc6c36aa2844049876baa01eda00e3-ModemManager.service-1KPXNg', '/tmp/systemd-private-f2bc6c36aa2844049876baa01eda00e3-fwupd.service-WV32yf', '/tmp/systemd-private-f2bc6c36aa2844049876baa01eda00e3-systemd-logind.service-8dUslg', '/tmp/systemd-private-f2bc6c36aa2844049876baa01eda00e3-systemd-resolved.service-c3h2qj', '/tmp/systemd-private-f2bc6c36aa2844049876baa01eda00e3-systemd-timesyncd.service-OEfLRe', '/tmp/systemd-private-f2bc6c36aa2844049876baa01eda00e3-upower.service-oCTqBh']
/home/test/test目录中独有2个文件: ['/home/test/test/config', '/home/test/test/user_sum.py']
名称相同但不一致的有0个文件: []