1

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.

1
  • 5
    Have you tried moving flist = [] outside / above your for ips in npat loop? Commented Feb 16, 2019 at 23:29

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.