I am having some trouble generating the sum of some numbers when reading a file line-by-line. I want to calculate the sum from a separate file without saving the numbers as a list. Is there an easy command for this? My code looks like this:
def imput(filename):
with open(filename, 'r') as f: #open the file
for line in f:
input('sample.txt')`
The file 'sample.txt' consists of the numbers 1,2,4,6,8
and when I use the print function I get:
print(line)
1
2
4
6
8