2

I have a python script, with a nested for loop

    HostList = file('trylist.txt')
    DestHostList = file('trylist2.txt')
    for DestHost in DestHostList:
            DestHost = DestHost.strip()
            for HostName in HostList:
                    HostName = HostName.strip()
                    try:
                            if DestHost!=HostName:
                                    print HostName,DestHost

                            print "one loop finishes"
                    except Exception, e:
                            ExceptionHost.write(HostName+' -  '+DestHost+':    '+str(e)+'\n')       
                            print "exception"
                            #traceback.print_exc(file=sys.stdout)

The outer for loop appears to be only executed once. What could be the potential problem?

14
  • 1
    DestHostList has only one element? Commented Apr 22, 2013 at 3:38
  • many elements, not just one Commented Apr 22, 2013 at 3:39
  • 1
    If the outer loop only loops once, then that list has only one element. The inner loop is iterating over HostList. Commented Apr 22, 2013 at 3:40
  • but DestHostList has 3 elements Commented Apr 22, 2013 at 3:49
  • Sorry @DSM, I may have missed an indentation in a recent edit I was trying to help with... Commented Apr 22, 2013 at 3:54

2 Answers 2

4

The outer loop executes more than once, but only in the first iteration it has things to do. In the rest, the inner loop does not execute, leaving you with the impression it's only running once.

The problem is that you open the first file, trylist.txt, and read it entirely in the first iteration of the outer loop. On the second iteration, the file object (which is iterator-like) has already been "consumed".

Try:

HostList = file('trylist.txt').readlines()  # now it's a list of lines, not an iterator
DestHostList = file('trylist2.txt')
for DestHost in DestHostList:
    DestHost = DestHost.strip()
    for HostName in HostList:

If the file is huge and you want to avoid storing it in memory, you can reopen it every time:

DestHostList = file('trylist2.txt')
for DestHost in DestHostList:
    DestHost = DestHost.strip()
    HostList = file('trylist.txt')
    for HostName in HostList:

Also, it is good practice to open files in python using a with statement.

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

3 Comments

And you want to open the file with open, not with file.
@Matthias I know using open is the recommended way, but I don't see why the OP should worry about such a minor detail, when his real concern is making his simple script work...
Make something work is good. Make something work as it is intended to work is better.
0

The problem is that you have exhausted the second file in the inner loop after the first pass.

As I can see you were trying to troubleshoot the issue with try ... except .... But since the file read operation is outside the block, you missed it.

Here is a solution with reading in advance all file content. I hope it will help:

host_list = file("trylist.txt").readlines()
dest_host_list = file("trylist2.txt").readlines()

for h in host_list:
    for d in dest_host_list:
        h,d = h.strip(),d.strip()
        if h != d:
            print h,d

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.