David542
David542

Reputation: 110093

Python linecount in a file

For the following:

def linecount(filename):
    count = 0
    for x in open(filename):
        count += 1
    return count

How does the script 'know' that each line is a separate element? For 'file' type, is this how it is essentially separated -- by line? Thank you

Upvotes: 1

Views: 6573

Answers (3)

jmurdz
jmurdz

Reputation: 1

I've been disappointed with the speed of the Python approach. I've resorted to calling wc.exe through os.popen to get the fastest possible result:

int(os.popen("wc -l " + filename).read().strip().split()[0])

Upvotes: 0

Blender
Blender

Reputation: 298106

Because when you iterate over a file object, it acts as if you are iterating over:

open(filename).readlines()

but without storing to memory (which is good for huge files).


The Python documentation explains this in more detail, but here's the juicy stuff:

>>> f = open('foo.txt', 'r')
>>> f.readlines()
['This is the first line of the file.\n', 'Second line of the file\n']

An alternative approach to reading lines is to loop over the file object. This is memory efficient, fast, and leads to simpler code:

>>> for line in f:
        print line,

This is the first line of the file.
Second line of the file

Upvotes: 4

Devin Jeanpierre
Devin Jeanpierre

Reputation: 95516

Yes. The file class reads in data from the file, and yields a new line whenever it encounters a linebreak. (You can find the actual implementation of this in iobase.c starting around line 551)

Upvotes: 2

Related Questions