how to break out of only one nested loop how to break out of only one nested loop python python

how to break out of only one nested loop


break and continue apply to the innermost loop.

The issue is that you open the second file only once, and therefore it's only read once. When you execute for y in file2.readlines(): for the second time, file2.readlines() returns an empty iterable.

Either move file2 = open(filename2, 'r') into the outer loop, or use seek() to rewind to the beginning of file2.


You need to parse the numeric strings to their corresponding integer values.

You can use int('hoge') as follows.

import sysfilename1 = sys.argv[1]filename2 = sys.argv[2]with open(filename1) as file1:    for x in file1:        with open(filename2) as file2:            col = x.strip().split()            for y in file2:                col2 = y.strip().split()                if col[1] == col2[1] and int(col[3]) < int(col2[2]):                    print x