Why loop overwriting my file instead of writing after text?
<pre><code>i = 1 # keep track of file number
directory = '/some/directory/'


for i in range(1, 5170): #number of files in directory
    filename = directory + 'D' + str(i) + '.txt'
    input = open(filename)
    output = open('output.txt', 'w')
    input.readline() #ignore first line
    for g in range(0, 7): #write next seven lines to output.txt
        output.write(input.readline())

    output.write('\n') #add newline to avoid mess
    output.close()
    input.close()
    i = i + 1
</code></pre>

<p>I have this code, and i am trying to get one file and rewrite it to output.txt, but when i want to attach next file, my code overwrite older file that has been attached. In result when code is complete i have something like this:</p>

<pre><code>dataA[5169]=26
dataB[5169]=0
dataC[5169]=y
dataD[5169]='something'
dataE[5169]=x
data_date[5169]=2012.06.02</code></pre>

<p>Instead of datas ranging from files 0 to 5169. Any tips how to fix it?</p>
