How to count lines in a text file ?
Alex Martelli
aleaxit at yahoo.com
Mon Sep 20 09:29:18 EDT 2004
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 :)
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
Alex
More information about the Python-list
mailing list