Total No. of "Records" in a File?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Aug 26 04:51:32 EDT 2008


W. eWatson a écrit :
> 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.
> 
The whole idea is that you should learn what a DBMS is good for, IMHO.



More information about the Python-list mailing list