Question: Write a function file_sums() that has two parameters, say, infile and outfile, that are bound to file names. Each line in infile has one or
Write a function file_sums() that has two parameters, say, infile and outfile, that are bound to file names. Each line in infile has one or more integers separated by whitespace. This function writes as a line to outfile the sum of the integers on the corresponding line of infile. Open both files in a with statement. Use a list comprehension to convert the split line into a list of integers. Note that function sum(), passed a list of numbers, returns the sum of those numbers. Recall that file method write() takes a single, string argument; that argument must end with a new line (use ' ') for what is written to be a complete line (so that what is written next is on the following line). For example, if the contents of the input file data.txt (available from the assignment page, and which you should use as a test file) are 3 12 72 32 54 29 78 31 28 9 7 83 then the contents of the output file become 87 86 29 146 90
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
