How to count lines in a text file ?
Christos TZOTZIOY Georgiou
tzot at sil-tec.gr
Wed Sep 22 08:45:52 EDT 2004
On Mon, 20 Sep 2004 15:29:18 +0200, rumours say that aleaxit at yahoo.com
(Alex Martelli) might have written:
>Ling Lee <janimal at mail.trillegaarden.dk> wrote:
>> Oh I just did it.
>>
>> Just used the line:
>>
>> print "%d lines in your choosen file" % len(open("test.txt").readlines())
>>
>> Thanks though :)
[Alex]
>You're welcome;-). However, this approach reads all of the file into
>memory at once. If you must be able to deal with humungoug files, too
>big to fit in memory at once, try something like:
>
>numlines = 0
>for line in open('text.txt'): numlines += 1
And a short story of premature optimisation follows...
Saw the plain code above and instantly the programmer's instinct of
optimisation came into action... we all know that C loops are faster
than python loops, right? So I spent 2 minutes of my time to write the
following 'clever' function:
def count_lines(filename):
fp = open(filename)
count = 1 + max(enumerate(fp))[0]
fp.close()
return count
Proud of my programming skills, I timed it against another function
containing Alex' code. Guess what? My code was slower... (and I should
put a try: except Value: clause to cater for empty files)
Of course, on second thought, the reason must be that enumerate
generates one tuple for every line in the file; in any case, I'll mark
this rule:
C loops are *always* faster than python loops, unless the loop does
something useful ;-) in the latter case, timeit.py is your friend.
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
More information about the Python-list
mailing list