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(a, b): return a + b def main(): with open('source.txt', 'r') as source_file: with open('target.txt', 'w') as target_file: for line in source_file: target_file.write(line) a, b = line.split() target_file.write(add(a, b)) target_file.write('\n') if __name__ == '__main__': main()

Questions about programming?Chat with your personal AI assistant