How to count lines in a text file ?
Alex Martelli
aleaxit at yahoo.com
Mon Sep 20 09:29:18 EDT 2004
More information about the Python-list mailing list
Mon Sep 20 09:29:18 EDT 2004
- Previous message (by thread): How to count lines in a text file ?
- Next message (by thread): How to count lines in a text file ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message (by thread): How to count lines in a text file ?
- Next message (by thread): How to count lines in a text file ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list