我必须编写python代码:
例如,我有一个这样的文本文件:
KeepAlive on
Listen 80
TCP On
以及正常的httpd.conf
文件。
我想检查并比较每个行字段,如果配置正确,则打印keepalive is ok
例如。
我写了这个:
d = []
with open("config.txt") as CFGF:
for line in CFGF:
key, val = line.split()
c = key, val
d.extend(c)
with open("httpd.conf") as f:
j = 0
for i in d:
for line in f:
ls = line.strip()
if d[j] in line:
if d[j + 1] in line:
print(line.rsplit())
j += 1
答案 0 :(得分:0)
最后我写了一些有用的东西(下图),但还有一件事情就是存在(匹配确切的单词和忽略的情况)。任何帮助
l = []
with open('config.txt') as cfg:
for line in cfg:
l.extend(line.split())
a, b = zip(*(s.split("~") for s in l))
for w in range(len(a)):
with open('httpd.conf') as apache:
for lines in apache:
if (a[w]) in lines and not lines.startswith("#") and not lines.__contains__("#"):
if (b[w]) in lines:
print(lines.rstrip())