file - How do you count number of characters from each lines then add them all up? -


i have given question write function "that returns count of number of characters in file name given parameter."

so if file called "data.txt" contains "hi there!" , printed using codes below, return value of 10. (which correct)

"""attemping question 7.    author: ark    date: 28/04/2015 """  def file_size(filename): """extracts word line""" filename = open(filename, 'r') line in filename:     result = len(line) #count number of characters in line.     return result 

however, let have made file called "data2.txt" , contains

eeeee dddd ccc bb 

if print out give value of 6. so, challenge starts here.. can coding read lines , add them up?

print(file_size("data2.txt"))

expected 16 words (?)

you must sum lengths of lines, right return length of first line.

also, must strip trailing newline if it's there. should work:

def character_count(filename):     open(filename) f:         return sum(len(line.rstrip("\n")) line in f)