Variable assignments inside for and then if loops
<p>For the following working code, I resorted to creating a class instance to store the name variable of my file output [a_string] and the file object itself [f_object]. I found that variables assigned inside the first if statement did not appear in the scope inside the following elif statement.</p>

<pre><code>#Text file splitter, data bewteen the '*' lines are copied into new files.

class Output_file():
    def __init__(self,a_string='none',f_object='none'):
        self.name=a_string
        self.foutput=f_object

outputfile=Output_file()

n=0
filehandle=open('original file.txt')
for line in filehandle:

    if line[0]=='*': #find the '*' that splits the rows of data
        n+=1
        outputfile.name = 'original file_'+str(n)+'.txt'
        outputfile.foutput= open(outputfile.name,'w')
        outputfile.foutput.write(line)

    elif len(line.split()) ==5 and n &gt; 0: #make sure the bulk data occurs in blocks of 5
        outputfile.foutput= open(outputfile.name,'a+')
        outputfile.foutput.write(line)


outputfile.foutput.close()
</code></pre>

<p>Do I have to use a class instance to store the file name and object or is there a better way? </p>
