Why is my IF statement being ignored?

Discussion created by mustol on Mar 1, 2011
Latest reply on Mar 5, 2011 by JScheirer-esristaff
I'm sure there is a simple answer, but it seems to be beyond my limited Python knowledge. I have some very large text files, in which data is arranged in columns. One of the attributes is 'Month' and I want to create a new txt file for each month. My code is as follows:

infile = open(r"P:\Months_Test.txt")

mth = 1

for inlines in infile.readlines():
    if str(mth) in inlines[-2:]:
       outfile = open(r"P:\Month_" + str(mth) + ".txt", 'a')
       mth += 1


If I run this code without the ELSE statement, it correctly outputs the month 1 data into the month 1 text file. However, as soon as I add the ELSE statement, it skips the IF statement completely and just counts the number of lines in the text file (I checked by printing mth). What am I doing wrong?