I have a list of Python objects of which there are two types, Terrys and not Terrys. The header for each of these is different but consistent between types. I need to collate these into a single file (with other processing as well), one file for Terrys and one for the rest. The code looks something like this (note I'm using python 2.6 so can't use the python 2.7 + approach of a single with statement to open multiple files at once):
terry_header_done = [False]
not_terry_header_done = [False]
with open('terry.txt','w') as f_terry:
with open('not_terry.txt','w') as f_not_terry:
for python in pythons:
if python.firstname == 'Terry':
header_done = terry_header_done
fout = f_terry
else:
header_done = not_terry_header_done
fout = f_not_terry
with open(python.input_fname,'r') as fin:
line = fin.readline()
if not header_done[0]:
fout.write(line)
header_done[0] = True
<more processing that will write lines into fout>
I'm using a single element list in order to get a reference like behaviour so that e.g. terry_header_done[0] will change if header_done[0] is changed.
Is there a simpler and more pythonic way to achieve this?