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 copy(sourceFileName, targetFileName): f = open(sourceFileName, 'r') g = open(targetFileName, 'w') nextLine = f.readline() while nextLine != "": g.write(nextLine) nextLine = f.readline() f.close() g.close() def sum(sourceFileName, targetFileName): f = open(sourceFileName, 'r') g = open(targetFileName, 'w') nextLine = f.readline() s = 0 while nextLine != "": nextLine = f.readline() s = s + int(nextLine) f.close() g.write("The sum of the numbers in the source file is ") g.writeline(s) g.close()

Questions about programming?Chat with your personal AI assistant