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. use a function to add the numbers in the source file and write to target file the sum.

def add_numbers_in_file(source_file, target_file): with open(source_file) as f: with open(target_file) as t: while True: line = f.readline() if not line: break line_int = int(line) total += line_int t.write(line) f.close() t.close() return total print(add_numbers_in_file("source.txt", "target.txt"))

Questions about programming?Chat with your personal AI assistant