How to count lines in a text file ?

Andrew Dalke adalke at
Thu Sep 23 19:12:43 CEST 2004

Alex Martelli wrote:
> If you tell it to count non-lines too (pieces that don't end with an
> endline marker), it does, of course:

My reply was meant to be a bit of a jest, pointing out that
I'm using Python's definition of a line.  Otherwise if
lines must end with a newline then the method should be
named "readlines_and_any_trailing_text()"

Since you used

for line in file('/usr/share/dict/words'): numlines+=1

as a way to count lines, I assumed you would agree with
Python's definition as a reasonable way to count the
number of lines in a file and that your previous post
(on the behavior of wc) was meant more as a rhetorical
way to highlight the ambiguity than as an assertion of
general correctness.

> I suspect somebody who asks the subject question wants to reproduce wc's
> counting behavior.

Really?  I was actually surprised at what wc does.  I didn't
realize it only did a "\n" character count.  The other programs
I know of number based on the start of line rather than end
of line.

% echo -n "blah" > blah.txt
% less blah.txt
  (then press "=")
blah.txt lines 1-1/1 byte 4/4 (END)   (press RETURN)

% echo -n "" | perl -ne '$line++; END{$line+=0;print "$line\n"}'
% echo -n "blah" | perl -ne '$line++; END{$line+=0;print "$line\n"}'

% echo -n "" | awk 'END {print NR}'
% echo -n "blah" | awk 'END {print NR}'

% echo -n "blah" | grep -n "blah"

 > Of course, it _is_ an imprecise spec they're giving.


				dalke at

More information about the Python-list mailing list