I have a problem with Python not properly looping over the lines in a file. In the first block it loops over the lines properly and displays each line in the file. However in the second block it only does the first line of the file and then stops. The output of block 2 should be the same but it is not. It is driving me crazy.
BLOCK-1
f = open(filename, 'r')
for line in f:
print line,
f.close()
.
BLOCK-2
f = open(filename, 'r')
for line in f:
match = re.search(r'^(.*)$', line)
if match:
print match.group(1)
else:
return "DOES NOT MATCH"
Your block-2 code has a return statement. Any line that will not match your regex will break the loop.