read from source file and write to target file line by line until the end of the source file. add the numbers in the source file and write to target file the sum.
def add_source_target(source, target): with open(source) as source, open(target, 'w') as target: sum = 0 for line in source: sum += int(line) target.write(str(sum)) add_source_target('source.txt', 'target.txt')