[Tutor] Dictionary File character reader

Alan Gauld alan.gauld at btinternet.com
Tue May 10 10:15:37 CEST 2011


"Noah Hall" <enalicho at gmail.com> wrote 

> What you want is something that takes the length of each line, 
> and add it to the sum. A simple way would be to do
>>>sum(len(line) for line in file)

And if you just want the total count for the file an even 
simpler way is to use file.read()

count = len(file.read())


One other thing to consider is whether you want to include 
line feed characters in the count...

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list