【发布时间】:2022-12-24 22:37:22
【问题描述】:
我正在比较 2 个 txt 文件,我想在差异行之前添加包含此行的接口名称,如下所示:
第一个文件包含:
!CfgFileCrc:f4fcebea
!Software Version V800R021C00SPC100
!Last configuration was updated at 2022-04-07 10:16:05 UTC
!Last configuration was saved at 2022-04-09 21:00:41 UTC
!MKHash
info-center filter-id bymodule-alias system hwSecurityRisk
info-center loghost source LoopBack1
set service-mode forwarding-mode enhance
interface GigabitEthernet4/0/14
shutdown
interface GigabitEthernet4/0/15
negotiation auto
undo shutdown
interface GigabitEthernet4/0/16
negotiation auto
undo shutdown
第二个文件包含:
!CfgFileCrc:f4fcebea
!Software Version V800R021C00SPC100
!Last configuration was updated at 2022-04-07 10:16:05 UTC
!Last configuration was saved at 2022-04-09 21:00:41 UTC
!MKHash
info-center filter-id bymodule-alias system hwSecurityRisk
info-center loghost source LoopBack1
set service-mode forwarding-mode enhance
interface GigabitEthernet4/0/14
shutdown
interface GigabitEthernet4/0/15
negotiation auto
description CEM-Smart-Care-PS
undo shutdown
interface GigabitEthernet4/0/16
negotiation auto
description CEM-Smart-Care-PS
undo shutdown
代码如下:
def files(Devices):
doc = open('C:/Users/Ahmed Shouaib/Desktop/Difference/test/10-4-2022/'+Devices+'.txt', 'r')
dox = open('C:/Users/Ahmed Shouaib/Desktop/Difference/test/7-4-2022/'+Devices+'.txt', 'r')
f1 = [x for x in doc.readlines()]
f2 = [x for x in dox.readlines()]
with open('C:/Users/Ahmed Shouaib/Desktop/Difference/test/result/' + Devices + '.txt', 'w') as new:
GGG = ['CfgFileCrc', 'Last configuration was','MKHash' , 'username ftpuser password']
for line in f1:
if line not in f2 and not any(x in line for x in GGG):
new.write("+"+line + '\n')
GGG = ['CfgFileCrc','Last configuration was','MKHash','username ftpuser password']
XX=1
for line in f2:
if line not in f1 and not any(x in line for x in GGG):
if ( XX == 1):
new.write('-' * 80 + '\n'+ '\n')
XX = 0
new.write("-"+line + '\n')
doc.close()
dox.close()
files('10.0.130.71')
代码结果如下:
+ description CEM-Smart-Care-PS
+ description CEM-Smart-Care-PS
我需要的代码结果:
interface GigabitEthernet4/0/15
+ description CEM-Smart-Care-PS
interface GigabitEthernet4/0/16
+ description CEM-Smart-Care-PS
【问题讨论】:
标签: python difference txt