I cant spot what is going on here with this nested loop. The inner (for line in temp:) loop runs great but outer for loop (for cnt in range) only runs once, so that temp is only appended to infile once.
with open("temp_inv.ext") as temp:
with open(infile, 'a') as of:
for cnt in range(1, sample_cnt):
for line in temp:
l = None
if "INVERSE_MODELING" in line:
l = line.replace(smp_num[0], smp_num[cnt])
if "solutions" in line:
l = line.replace(smp_num[0], smp_num[cnt])
if "netpath" in line:
l = line.replace(smp_num[0], smp_num[cnt])
if l is None:
of.write(line)
else: of.write(l)
I have checked that 'sample_cnt' is an integer and 'smp_num' is a valid array.
What am I missing?
EDIT:
To clarify for others that want to use a similar method -
I want to append 'temp' to 'of' after mangling 'temp' by changing a string based on the 'smp_num' array. Addition of the {temp.seek(0)} answered my problem, I had missed that the outer loop was still inside the {with} statement, thus needed to seek before running again.
You need to use seek() to rewind the file once you read all the lines. Use
temp.seek(0)
somewhere in the outer loop