with open('file2.txt') as protein:
    with open('file1.txt') as position:
        with open('test.result', 'r+') as fallout:
            writer = csv.writer(fallout, delimiter=' ')
for rowinprot in csv.reader(protein, delimiter=' '):
    position.seek(0)
valid = True
for rowinpos in csv.reader(position, delimiter=' '):
    if rowinprot[2] == rowinpos[0]:
        if not (int(rowinprot[4]) < int(rowinpos[1]) or int(rowinprot[3]) >
            int(rowinpos[2])):
            valid = False
if valid:
    writer.writerow(rowinprot)
How to print out only records with intervals, from one file, not overlapping with those from another file