Total No. of "Records" in a File?

Grant Edwards grante at visi.com
Sat Aug 23 13:48:36 EDT 2008


On 2008-08-23, W. eWatson <notvalid2 at sbcglobal.net> 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?

If the lines are fixed lengh (e.g. always 12 bytes long), then
you can use os.stat() or os.fstat() to find the size of the
file.  Divide the size of the file by the number of bytes in a
line, and you get the number of lines.

-- 
Grant





More information about the Python-list mailing list