Total No. of "Records" in a File?

Fredrik Lundh fredrik at pythonware.com
Sat Aug 23 19:51:38 CEST 2008


W. eWatson wrote:

> I have an ordinary text file with a CR at the end of a line, and two 
> numbers in each line. Is there some way to determine the number of lines 
> (records) in the file before I begin reading it?

In the general case, no.  A file is just a bunch of bytes.  If you know 
that all lines have exactly the same length, you can of course fetch the 
file size and divide by the line size, but that doesn't work for 
arbitrary files.

Why do you need to know the number of lines before reading it, btw?

</F>




More information about the Python-list mailing list