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)
Your issue is that you open in write mode. To append to file you want to use append. See here.