Cody Bable

Generation

generate functionSun, 23 Apr 2023

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.

with open("source.txt", "r") as sourcefile: with open("target.txt", "w") as targetfile: sum = 0 while True: read = sourcefile.readline() if not read: break sum += int(read) targetfile.write(str(sum))

Questions about programming?Chat with your personal AI assistant