check if any of the items of a list is in a line of a file, if not, then send mail

Viewed 56

I have a list, I need to check whether all the elements of this list are in the .cfg file, if one of these elements is not found in the file, it is necessary to write the missing element in the file_diff.html file and run the sender() function. If all items from the list have been found, run the main() function. I implemented it like this, but only the first element(v[0]) is checked. But elif is also processed as if nothing from the list is in the file.

    def validator():
                   v = ['ip arp inspection',
                       f'ip arp inspection vlan {l[2]}',
                       'ip dhcp snooping',
                       f'ip dhcp snooping vlan {l[2]}',
                       'ip dhcp snooping information option'
                       ]   
                   with open(f'/home/tftp/cfg/{today}/all/edgecore/{l[0]}.cfg', 'r') as val:
                       for line in val:
                           if all(word in line for word in v): 
                              main()
                       elif not any(word in line for word in v): 
                           with open('file_diff.html', 'w') as noname:
                              title = f"The configuration on switch {l[0]} do not valid"
                              noname.write(title)
                              sender()
1 Answers

I need to check whether all the elements of this list are in the .cfg file

for line in val:
  if all(word in line for word in v): ...

You're checking if the line of the file having your strings not the whole file. For this question I recommend using val.read():

if all(word in val.read() for word in v): ...

If problem not in this, add error code and problem part of it.

Related