I am trying to write to a different file every N iterations in a loop, and store everything that happened between that batch. I can accomplish this by using lists. For example,
import os
def write_to_file(data, fileName):
file = os.path.join('/home/user/data', fileName)
with open(file, 'a') as f:
f.write(str(data))
TEMP = []
for i in range(50):
TEMP.append(i)
if i != 0 and i % 10 == 0:
write_to_file(TEMP, 'data{}.txt'.format(i))
TEMP = []
This will effectively write into a different file every 10th iteration , as expected, like the following:
File #1: [0, ... , 10]
File #2: [11, ..., 20]
...
But is there any other way of doing this without having to use a list
?
I am not having performance issues or anything but I feel like there most be another way of doing this without having to explicitly call a list
.