1
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

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:

dataA[5169]=26
dataB[5169]=0
dataC[5169]=y
dataD[5169]='something'
dataE[5169]=x
data_date[5169]=2012.06.02

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

zuberuber
  • 3,851
  • 2
  • 16
  • 20

2 Answers2

8

You probably want to open output.txt before your for loop (and close it after). As it is written, you overwrite the file output.txt everytime you open it. (an alternative would be to open for appending: output = open('output.txt','a'), but that's definitely not the best way to do it here ...

Of course, these days it's better to use a context manager (with statement):

i = 1 # keep track of file number <-- This line is useless in the code you posted
directory = '/some/directory/'  #<-- os.path.join is better for this stuff.
with open('output.txt','w') as output:

    for i in range(1, 5170): #number of files in directory
        filename = directory + 'D' + str(i) + '.txt'
        with open(filename) as input:

            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
        i = i + 1   #<---also useless line in the code you posted
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • @sr2222 -- That's a variant of the same thing I just said :) – mgilson Oct 25 '12 at 13:21
  • Fair enough. Though with `with`, your housekeeping is done for you (including exception states, which aren't handled at all here). – Silas Ray Oct 25 '12 at 13:22
  • i is not useless, i use it for something else – zuberuber Oct 25 '12 at 13:24
  • 1
    @sr2222 -- You're right of course. (I was in the middle of adding that when you suggested it). I just want to make it clear that it was a *logic* error, not the lack of using a very powerful python feature which caused this bug. – mgilson Oct 25 '12 at 13:25
  • 1
    @zuberuber -- Fair enough, but it is useless as far as the code you posted is concerned. :-). As far as I knew, you might have a misunderstanding about how the loops work and I thought that pointing out that `i` wasn't used after you increment it might help you out. Also note that the same could be written as `i += 1`. – mgilson Oct 25 '12 at 13:30
  • upvoted but it is not what i was looking for. @learner answer my question. thanks anyway for great tip – zuberuber Oct 25 '12 at 13:31
2

Your issue is that you open in write mode. To append to file you want to use append. See here.

Community
  • 1
  • 1
learner
  • 1,895
  • 2
  • 19
  • 21