Total No. of "Records" in a File?

W. eWatson notvalid2 at sbcglobal.net
Sat Aug 23 15:45:24 EDT 2008


Fredrik Lundh wrote:
> 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>
> 
Actually, it was a matter of curiosity, and maybe absent mindedness. I was 
envisioning a program where I might want to run up and down a file a lot, 
sometimes deleting a record interactively at the request of the user. 
However, I wanted to keep him alert to the total number of records 
remaining. However, in retrospect, I more likely do this with files in a 
folder. I also want him to be able to skip around in the Win OS folder by 
saying something like go forward 3 files. I'd like not to have to read all 
the files between the two points. The whole idea needs some more thinking.

-- 
            Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

              (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
               Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

                     Web Page: <www.speckledwithstars.net/>



More information about the Python-list mailing list