Question #36408

Hi there,

I'm trying to do the following in Python 2.7:

- Open a file containing numbers, calculate the sum of those numbers

- Open a second file containing numbers, calculate the sum of those numbers

- Add the total and output the answer to a third file.

Any help you can offer would be most appreciated

Thank you

Rory
1

Expert's answer

2013-11-01T13:01:36-0400
f1 = open('file1.txt', 'r')
f2 = open('file2.txt','r')
summ = 0
# fist file all nubers in one row
line = f1.readline()  #read line
numbers = [int(i) for i in line.split(' ')]  #get numbers
summ = sum(numbers)  #sum
#second file one number per row
numbers = [int(i) for i in f2.readlines()]  #read all lines and get numbers
summ += sum(numbers)  #sum
print (summ)
f3 = open('sum.txt','w')
f3.write(str(summ))
f3.close()
#closing files
f1.close()
f2.close()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS