How to count lines in a text file ?

Bengt Richter bokr at oz.net
Wed Sep 22 16:22:31 EDT 2004


On Wed, 22 Sep 2004 19:48:21 GMT, Andrew Dalke <adalke at mindspring.com> wrote:

>Bengt Richter wrote:
>> or, if you _are_ willing to read in the whole file,
>> 
>>     open('text.txt').read().count('\n')
>
>Except the last line might not have a terminal newline.
>
I _knew_ I should have mentioned that ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list