【发布时间】:2020-10-11 17:42:31
【问题描述】:
我的 expected_cmd.txt(比如 f1)是
mpls ldp
snmp go
exit
而我的configured.txt(比如f2)是
exit
这是我正在尝试的代码,在 f2 中搜索 f1 的所有行
with open('expected_cmd.txt', 'r') as rcmd, open('%s.txt' %configured, 'r') as f2:
for line in rcmd:
print 'line present is ' + line
if line in f2:
continue
else:
print line
所以基本上我正在尝试从第一个文件中打印第二个文件中不存在的行。 但是使用上面的代码,我得到的输出为
#python validateion.py
line present is mpls ldp
mpls ldp
line present is snmp go
snmp go
line present is exit
exit
不知道为什么打印匹配的exit。
我也想知道在 python 中是否有一个内置函数可以做到这一点?
【问题讨论】:
标签: python file with-statement