Reputation: 87
I want to create list of result string based on if
-else
result, but currently only a 1-value string can be seen when running the loop.
Example:
for ips in npat:
ipnet = ips.strip()
print ("Processing ..... ", ipnet)
fgen = "grep " +ipnet+ " /mnt/hgfs/IRR/fgen.txt"
f2pat = re.findall(ipnet,fgen)
print ("\nCommand: ",fgen)
os.system(fgen)
print ("\n NEW NPATH: ",f2pat)
flist = []
if ipnet in f2pat:
flist.append("Grep Found")
print ("Result ", flist)
else:
flist.append("Grep NotFound")
print ("Result: ",flist)
flist -> ['Grep Found']
...Only 1 value is in the list in spite of ther should be multiple values. May I know your ideas?
Thanks.
Upvotes: 1
Views: 193
Reputation: 1758
it seems that flist = []
gets reintiallized in the loop. Move that variable alone above your for loop
. Hence the code becomes :
flist = []
for ips in npat:
ipnet = ips.strip()
print ("Processing ..... ", ipnet)
fgen = "grep " +ipnet+ " /mnt/hgfs/IRR/fgen.txt"
f2pat = re.findall(ipnet,fgen)
print ("\nCommand: ",fgen)
os.system(fgen)
print ("\n NEW NPATH: ",f2pat)
if ipnet in f2pat:
flist.append("Grep Found")
print ("Result ", flist)
else:
flist.append("Grep NotFound")
print ("Result: ",flist)
Credits to jedwards comment
Upvotes: 4